blob: a227e7a9b8b7b5dbfcfcaae9dce1ee9ef79393f1 [file] [log] [blame]
Thomas Gleixner45051532019-05-29 16:57:47 -07001// SPDX-License-Identifier: GPL-2.0-only
Joerg Roedelb6c02712008-06-26 21:27:53 +02002/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02003 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01004 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelb6c02712008-06-26 21:27:53 +02005 * Leo Duran <leo.duran@amd.com>
Joerg Roedelb6c02712008-06-26 21:27:53 +02006 */
7
Joerg Roedel101fa032018-11-27 16:22:31 +01008#define pr_fmt(fmt) "AMD-Vi: " fmt
Bjorn Helgaas5f226da2019-02-08 16:05:53 -06009#define dev_fmt(fmt) pr_fmt(fmt)
Joerg Roedel101fa032018-11-27 16:22:31 +010010
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010011#include <linux/ratelimit.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020012#include <linux/pci.h>
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -040013#include <linux/acpi.h>
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -040014#include <linux/amba/bus.h>
Wan Zongshun0076cd32016-05-10 09:21:01 -040015#include <linux/platform_device.h>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020016#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080017#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010019#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020020#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090021#include <linux/dma-mapping.h>
Christoph Hellwigfec777c2018-03-19 11:38:15 +010022#include <linux/dma-direct.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020023#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010024#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020025#include <linux/delay.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020026#include <linux/amd-iommu.h>
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010027#include <linux/notifier.h>
28#include <linux/export.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020029#include <linux/irq.h>
30#include <linux/msi.h>
Joerg Roedel3b839a52015-04-01 14:58:47 +020031#include <linux/dma-contiguous.h>
Jiang Liu7c71d302015-04-13 14:11:33 +080032#include <linux/irqdomain.h>
Joerg Roedel5f6bed52015-12-22 13:34:22 +010033#include <linux/percpu.h>
Joerg Roedel307d5852016-07-05 11:54:04 +020034#include <linux/iova.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020035#include <asm/irq_remapping.h>
36#include <asm/io_apic.h>
37#include <asm/apic.h>
38#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020039#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020040#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090041#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010042#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020043#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020044
45#include "amd_iommu_proto.h"
46#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020047#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020048
49#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
50
Joerg Roedel815b33f2011-04-06 17:26:49 +020051#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020052
Joerg Roedel307d5852016-07-05 11:54:04 +020053/* IO virtual address start page frame number */
54#define IOVA_START_PFN (1)
55#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
Joerg Roedel307d5852016-07-05 11:54:04 +020056
Joerg Roedel81cd07b2016-07-07 18:01:10 +020057/* Reserved IOVA ranges */
58#define MSI_RANGE_START (0xfee00000)
59#define MSI_RANGE_END (0xfeefffff)
60#define HT_RANGE_START (0xfd00000000ULL)
61#define HT_RANGE_END (0xffffffffffULL)
62
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020063/*
64 * This bitmap is used to advertise the page sizes our hardware support
65 * to the IOMMU core, which will then use this information to split
66 * physically contiguous memory regions it is mapping into page sizes
67 * that we support.
68 *
Joerg Roedel954e3dd2012-12-02 15:35:37 +010069 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020070 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010071#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020072
Sebastian Andrzej Siewior2cd10832018-03-22 16:22:41 +010073static DEFINE_SPINLOCK(amd_iommu_devtable_lock);
Sebastian Andrzej Siewior2bc00182018-03-22 16:22:35 +010074static DEFINE_SPINLOCK(pd_bitmap_lock);
Joerg Roedelb6c02712008-06-26 21:27:53 +020075
Joerg Roedel8fa5f802011-06-09 12:24:45 +020076/* List of all available dev_data structures */
Sebastian Andrzej Siewior779da732018-03-22 16:22:34 +010077static LLIST_HEAD(dev_data_list);
Joerg Roedel8fa5f802011-06-09 12:24:45 +020078
Joerg Roedel6efed632012-06-14 15:52:58 +020079LIST_HEAD(ioapic_map);
80LIST_HEAD(hpet_map);
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040081LIST_HEAD(acpihid_map);
Joerg Roedel6efed632012-06-14 15:52:58 +020082
Joerg Roedel0feae532009-08-26 15:26:30 +020083/*
84 * Domain for untranslated devices - only allocated
85 * if iommu=pt passed on kernel cmd line.
86 */
Joerg Roedelb0119e82017-02-01 13:23:08 +010087const struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010088
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010089static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010090int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010091
Bart Van Assche52997092017-01-20 13:04:01 -080092static const struct dma_map_ops amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +020093
Joerg Roedel431b2a22008-07-11 17:14:22 +020094/*
95 * general struct to manage commands send to an IOMMU
96 */
Joerg Roedeld6449532008-07-11 17:14:28 +020097struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020098 u32 data[4];
99};
100
Joerg Roedel05152a02012-06-15 16:53:51 +0200101struct kmem_cache *amd_iommu_irq_cache;
102
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200103static void update_domain(struct protection_domain *domain);
Joerg Roedel7a5a5662015-06-30 08:56:11 +0200104static int protection_domain_init(struct protection_domain *domain);
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100105static void detach_device(struct device *dev);
Joerg Roedel9003d612017-08-10 17:19:13 +0200106static void iova_domain_flush_tlb(struct iova_domain *iovad);
Joerg Roedeld4241a22017-06-02 14:55:56 +0200107
Joerg Roedel007b74b2015-12-21 12:53:54 +0100108/*
Joerg Roedel007b74b2015-12-21 12:53:54 +0100109 * Data container for a dma_ops specific protection domain
110 */
111struct dma_ops_domain {
112 /* generic protection domain information */
113 struct protection_domain domain;
114
Joerg Roedel307d5852016-07-05 11:54:04 +0200115 /* IOVA RB-Tree */
116 struct iova_domain iovad;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100117};
118
Joerg Roedel81cd07b2016-07-07 18:01:10 +0200119static struct iova_domain reserved_iova_ranges;
120static struct lock_class_key reserved_rbtree_key;
121
Joerg Roedel15898bb2009-11-24 15:39:42 +0100122/****************************************************************************
123 *
124 * Helper functions
125 *
126 ****************************************************************************/
127
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400128static inline int match_hid_uid(struct device *dev,
129 struct acpihid_map_entry *entry)
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100130{
Aaron Mabb6bccb2019-03-13 21:53:24 +0800131 struct acpi_device *adev = ACPI_COMPANION(dev);
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400132 const char *hid, *uid;
133
Aaron Mabb6bccb2019-03-13 21:53:24 +0800134 if (!adev)
135 return -ENODEV;
136
137 hid = acpi_device_hid(adev);
138 uid = acpi_device_uid(adev);
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400139
140 if (!hid || !(*hid))
141 return -ENODEV;
142
143 if (!uid || !(*uid))
144 return strcmp(hid, entry->hid);
145
146 if (!(*entry->uid))
147 return strcmp(hid, entry->hid);
148
149 return (strcmp(hid, entry->hid) || strcmp(uid, entry->uid));
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100150}
151
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400152static inline u16 get_pci_device_id(struct device *dev)
Joerg Roedele3156042016-04-08 15:12:24 +0200153{
154 struct pci_dev *pdev = to_pci_dev(dev);
155
Heiner Kallweit775c0682019-04-24 21:15:25 +0200156 return pci_dev_id(pdev);
Joerg Roedele3156042016-04-08 15:12:24 +0200157}
158
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400159static inline int get_acpihid_device_id(struct device *dev,
160 struct acpihid_map_entry **entry)
161{
162 struct acpihid_map_entry *p;
163
164 list_for_each_entry(p, &acpihid_map, list) {
165 if (!match_hid_uid(dev, p)) {
166 if (entry)
167 *entry = p;
168 return p->devid;
169 }
170 }
171 return -EINVAL;
172}
173
174static inline int get_device_id(struct device *dev)
175{
176 int devid;
177
178 if (dev_is_pci(dev))
179 devid = get_pci_device_id(dev);
180 else
181 devid = get_acpihid_device_id(dev, NULL);
182
183 return devid;
184}
185
Joerg Roedel15898bb2009-11-24 15:39:42 +0100186static struct protection_domain *to_pdomain(struct iommu_domain *dom)
187{
188 return container_of(dom, struct protection_domain, domain);
189}
190
Joerg Roedelb3311b02016-07-08 13:31:31 +0200191static struct dma_ops_domain* to_dma_ops_domain(struct protection_domain *domain)
192{
193 BUG_ON(domain->flags != PD_DMA_OPS_MASK);
194 return container_of(domain, struct dma_ops_domain, domain);
195}
196
Joerg Roedelf62dda62011-06-09 12:55:35 +0200197static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200198{
199 struct iommu_dev_data *dev_data;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200200
201 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
202 if (!dev_data)
203 return NULL;
204
Joerg Roedelf62dda62011-06-09 12:55:35 +0200205 dev_data->devid = devid;
Joerg Roedel30bf2df2017-05-15 16:25:03 +0200206 ratelimit_default_init(&dev_data->rs);
207
Sebastian Andrzej Siewior779da732018-03-22 16:22:34 +0100208 llist_add(&dev_data->dev_data_list, &dev_data_list);
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200209 return dev_data;
210}
211
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200212static struct iommu_dev_data *search_dev_data(u16 devid)
213{
214 struct iommu_dev_data *dev_data;
Sebastian Andrzej Siewior779da732018-03-22 16:22:34 +0100215 struct llist_node *node;
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200216
Sebastian Andrzej Siewior779da732018-03-22 16:22:34 +0100217 if (llist_empty(&dev_data_list))
218 return NULL;
219
220 node = dev_data_list.first;
221 llist_for_each_entry(dev_data, node, dev_data_list) {
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200222 if (dev_data->devid == devid)
Sebastian Andrzej Siewior779da732018-03-22 16:22:34 +0100223 return dev_data;
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200224 }
225
Sebastian Andrzej Siewior779da732018-03-22 16:22:34 +0100226 return NULL;
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200227}
228
Joerg Roedele3156042016-04-08 15:12:24 +0200229static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
230{
231 *(u16 *)data = alias;
232 return 0;
233}
234
235static u16 get_alias(struct device *dev)
236{
237 struct pci_dev *pdev = to_pci_dev(dev);
238 u16 devid, ivrs_alias, pci_alias;
239
Joerg Roedel6c0b43d2016-05-09 19:39:17 +0200240 /* The callers make sure that get_device_id() does not fail here */
Joerg Roedele3156042016-04-08 15:12:24 +0200241 devid = get_device_id(dev);
Arindam Nath5ebb1bc2018-09-18 15:40:58 +0530242
243 /* For ACPI HID devices, we simply return the devid as such */
244 if (!dev_is_pci(dev))
245 return devid;
246
Joerg Roedele3156042016-04-08 15:12:24 +0200247 ivrs_alias = amd_iommu_alias_table[devid];
Arindam Nath5ebb1bc2018-09-18 15:40:58 +0530248
Joerg Roedele3156042016-04-08 15:12:24 +0200249 pci_for_each_dma_alias(pdev, __last_alias, &pci_alias);
250
251 if (ivrs_alias == pci_alias)
252 return ivrs_alias;
253
254 /*
255 * DMA alias showdown
256 *
257 * The IVRS is fairly reliable in telling us about aliases, but it
258 * can't know about every screwy device. If we don't have an IVRS
259 * reported alias, use the PCI reported alias. In that case we may
260 * still need to initialize the rlookup and dev_table entries if the
261 * alias is to a non-existent device.
262 */
263 if (ivrs_alias == devid) {
264 if (!amd_iommu_rlookup_table[pci_alias]) {
265 amd_iommu_rlookup_table[pci_alias] =
266 amd_iommu_rlookup_table[devid];
267 memcpy(amd_iommu_dev_table[pci_alias].data,
268 amd_iommu_dev_table[devid].data,
269 sizeof(amd_iommu_dev_table[pci_alias].data));
270 }
271
272 return pci_alias;
273 }
274
Bjorn Helgaas5f226da2019-02-08 16:05:53 -0600275 pci_info(pdev, "Using IVRS reported alias %02x:%02x.%d "
276 "for device [%04x:%04x], kernel reported alias "
Joerg Roedele3156042016-04-08 15:12:24 +0200277 "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias),
Bjorn Helgaas5f226da2019-02-08 16:05:53 -0600278 PCI_FUNC(ivrs_alias), pdev->vendor, pdev->device,
Joerg Roedele3156042016-04-08 15:12:24 +0200279 PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias),
280 PCI_FUNC(pci_alias));
281
282 /*
283 * If we don't have a PCI DMA alias and the IVRS alias is on the same
284 * bus, then the IVRS table may know about a quirk that we don't.
285 */
286 if (pci_alias == devid &&
287 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) {
Linus Torvalds7afd16f2016-05-19 13:10:54 -0700288 pci_add_dma_alias(pdev, ivrs_alias & 0xff);
Bjorn Helgaas5f226da2019-02-08 16:05:53 -0600289 pci_info(pdev, "Added PCI DMA alias %02x.%d\n",
290 PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias));
Joerg Roedele3156042016-04-08 15:12:24 +0200291 }
292
293 return ivrs_alias;
294}
295
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200296static struct iommu_dev_data *find_dev_data(u16 devid)
297{
298 struct iommu_dev_data *dev_data;
Baoquan Hedf3f7a62017-08-09 16:33:41 +0800299 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200300
301 dev_data = search_dev_data(devid);
302
Baoquan Hedf3f7a62017-08-09 16:33:41 +0800303 if (dev_data == NULL) {
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200304 dev_data = alloc_dev_data(devid);
Sebastian Andrzej Siewior39ffe392018-03-22 16:22:33 +0100305 if (!dev_data)
306 return NULL;
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200307
Baoquan Hedf3f7a62017-08-09 16:33:41 +0800308 if (translation_pre_enabled(iommu))
309 dev_data->defer_attach = true;
310 }
311
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200312 return dev_data;
313}
314
Baoquan Hedaae2d22017-08-09 16:33:43 +0800315struct iommu_dev_data *get_dev_data(struct device *dev)
Joerg Roedel657cbb62009-11-23 15:26:46 +0100316{
317 return dev->archdata.iommu;
318}
Baoquan Hedaae2d22017-08-09 16:33:43 +0800319EXPORT_SYMBOL(get_dev_data);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100320
Wan Zongshunb097d112016-04-01 09:06:04 -0400321/*
322* Find or create an IOMMU group for a acpihid device.
323*/
324static struct iommu_group *acpihid_device_group(struct device *dev)
325{
326 struct acpihid_map_entry *p, *entry = NULL;
Dan Carpenter2d8e1f02016-04-11 10:14:46 +0300327 int devid;
Wan Zongshunb097d112016-04-01 09:06:04 -0400328
329 devid = get_acpihid_device_id(dev, &entry);
330 if (devid < 0)
331 return ERR_PTR(devid);
332
333 list_for_each_entry(p, &acpihid_map, list) {
334 if ((devid == p->devid) && p->group)
335 entry->group = p->group;
336 }
337
338 if (!entry->group)
339 entry->group = generic_device_group(dev);
Robin Murphyf2f101f2016-11-11 17:59:23 +0000340 else
341 iommu_group_ref_get(entry->group);
Wan Zongshunb097d112016-04-01 09:06:04 -0400342
343 return entry->group;
344}
345
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100346static bool pci_iommuv2_capable(struct pci_dev *pdev)
347{
348 static const int caps[] = {
349 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100350 PCI_EXT_CAP_ID_PRI,
351 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100352 };
353 int i, pos;
354
Gil Kupfercef74402018-05-10 17:56:02 -0500355 if (pci_ats_disabled())
356 return false;
357
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100358 for (i = 0; i < 3; ++i) {
359 pos = pci_find_ext_capability(pdev, caps[i]);
360 if (pos == 0)
361 return false;
362 }
363
364 return true;
365}
366
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100367static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
368{
369 struct iommu_dev_data *dev_data;
370
371 dev_data = get_dev_data(&pdev->dev);
372
373 return dev_data->errata & (1 << erratum) ? true : false;
374}
375
Joerg Roedel71c70982009-11-24 16:43:06 +0100376/*
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100377 * This function checks if the driver got a valid device from the caller to
378 * avoid dereferencing invalid pointers.
379 */
380static bool check_device(struct device *dev)
381{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400382 int devid;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100383
384 if (!dev || !dev->dma_mask)
385 return false;
386
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100387 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200388 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400389 return false;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100390
391 /* Out of our scope? */
392 if (devid > amd_iommu_last_bdf)
393 return false;
394
395 if (amd_iommu_rlookup_table[devid] == NULL)
396 return false;
397
398 return true;
399}
400
Alex Williamson25b11ce2014-09-19 10:03:13 -0600401static void init_iommu_group(struct device *dev)
Alex Williamson2851db22012-10-08 22:49:41 -0600402{
Alex Williamson2851db22012-10-08 22:49:41 -0600403 struct iommu_group *group;
Alex Williamson2851db22012-10-08 22:49:41 -0600404
Alex Williamson65d53522014-07-03 09:51:30 -0600405 group = iommu_group_get_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200406 if (IS_ERR(group))
407 return;
408
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200409 iommu_group_put(group);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600410}
411
412static int iommu_init_device(struct device *dev)
413{
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600414 struct iommu_dev_data *dev_data;
Joerg Roedel39ab9552017-02-01 16:56:46 +0100415 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400416 int devid;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600417
418 if (dev->archdata.iommu)
419 return 0;
420
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400421 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200422 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400423 return devid;
424
Joerg Roedel39ab9552017-02-01 16:56:46 +0100425 iommu = amd_iommu_rlookup_table[devid];
426
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400427 dev_data = find_dev_data(devid);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600428 if (!dev_data)
429 return -ENOMEM;
430
Joerg Roedele3156042016-04-08 15:12:24 +0200431 dev_data->alias = get_alias(dev);
432
Yu Zhaoc12b08e2018-12-06 14:39:15 -0700433 /*
434 * By default we use passthrough mode for IOMMUv2 capable device.
435 * But if amd_iommu=force_isolation is set (e.g. to debug DMA to
436 * invalid address), we ignore the capability for the device so
437 * it'll be forced to go into translation mode.
438 */
Joerg Roedelcc7c8ad2019-08-19 15:22:49 +0200439 if ((iommu_default_passthrough() || !amd_iommu_force_isolation) &&
Yu Zhaoc12b08e2018-12-06 14:39:15 -0700440 dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100441 struct amd_iommu *iommu;
442
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400443 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100444 dev_data->iommu_v2 = iommu->is_iommu_v2;
445 }
446
Joerg Roedel657cbb62009-11-23 15:26:46 +0100447 dev->archdata.iommu = dev_data;
448
Joerg Roedele3d10af2017-02-01 17:23:22 +0100449 iommu_device_link(&iommu->iommu, dev);
Alex Williamson066f2e92014-06-12 16:12:37 -0600450
Joerg Roedel657cbb62009-11-23 15:26:46 +0100451 return 0;
452}
453
Joerg Roedel26018872011-06-06 16:50:14 +0200454static void iommu_ignore_device(struct device *dev)
455{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400456 u16 alias;
457 int devid;
Joerg Roedel26018872011-06-06 16:50:14 +0200458
459 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200460 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400461 return;
462
Joerg Roedele3156042016-04-08 15:12:24 +0200463 alias = get_alias(dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200464
465 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
466 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
467
468 amd_iommu_rlookup_table[devid] = NULL;
469 amd_iommu_rlookup_table[alias] = NULL;
470}
471
Joerg Roedel657cbb62009-11-23 15:26:46 +0100472static void iommu_uninit_device(struct device *dev)
473{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400474 struct iommu_dev_data *dev_data;
Joerg Roedel39ab9552017-02-01 16:56:46 +0100475 struct amd_iommu *iommu;
476 int devid;
Alex Williamsonc1931092014-07-03 09:51:24 -0600477
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400478 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200479 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400480 return;
481
Joerg Roedel39ab9552017-02-01 16:56:46 +0100482 iommu = amd_iommu_rlookup_table[devid];
483
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400484 dev_data = search_dev_data(devid);
Alex Williamsonc1931092014-07-03 09:51:24 -0600485 if (!dev_data)
486 return;
487
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100488 if (dev_data->domain)
489 detach_device(dev);
490
Joerg Roedele3d10af2017-02-01 17:23:22 +0100491 iommu_device_unlink(&iommu->iommu, dev);
Alex Williamson066f2e92014-06-12 16:12:37 -0600492
Alex Williamson9dcd6132012-05-30 14:19:07 -0600493 iommu_group_remove_device(dev);
494
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200495 /* Remove dma-ops */
Bart Van Assche56579332017-01-20 13:04:02 -0800496 dev->dma_ops = NULL;
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200497
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200498 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600499 * We keep dev_data around for unplugged devices and reuse it when the
500 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200501 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100502}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100503
Andrei Dulea7f1f1682019-09-13 16:42:30 +0200504/*
505 * Helper function to get the first pte of a large mapping
506 */
507static u64 *first_pte_l7(u64 *pte, unsigned long *page_size,
508 unsigned long *count)
509{
510 unsigned long pte_mask, pg_size, cnt;
511 u64 *fpte;
512
513 pg_size = PTE_PAGE_SIZE(*pte);
514 cnt = PAGE_SIZE_PTE_COUNT(pg_size);
515 pte_mask = ~((cnt << 3) - 1);
516 fpte = (u64 *)(((unsigned long)pte) & pte_mask);
517
518 if (page_size)
519 *page_size = pg_size;
520
521 if (count)
522 *count = cnt;
523
524 return fpte;
525}
526
Joerg Roedel431b2a22008-07-11 17:14:22 +0200527/****************************************************************************
528 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200529 * Interrupt handling functions
530 *
531 ****************************************************************************/
532
Joerg Roedele3e59872009-09-03 14:02:10 +0200533static void dump_dte_entry(u16 devid)
534{
535 int i;
536
Joerg Roedelee6c2862011-11-09 12:06:03 +0100537 for (i = 0; i < 4; ++i)
Joerg Roedel101fa032018-11-27 16:22:31 +0100538 pr_err("DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200539 amd_iommu_dev_table[devid].data[i]);
540}
541
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200542static void dump_command(unsigned long phys_addr)
543{
Tom Lendacky2543a782017-07-17 16:10:24 -0500544 struct iommu_cmd *cmd = iommu_phys_to_virt(phys_addr);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200545 int i;
546
547 for (i = 0; i < 4; ++i)
Joerg Roedel101fa032018-11-27 16:22:31 +0100548 pr_err("CMD[%d]: %08x\n", i, cmd->data[i]);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200549}
550
Joerg Roedel30bf2df2017-05-15 16:25:03 +0200551static void amd_iommu_report_page_fault(u16 devid, u16 domain_id,
552 u64 address, int flags)
553{
554 struct iommu_dev_data *dev_data = NULL;
555 struct pci_dev *pdev;
556
Sinan Kayad5bf0f42017-12-19 00:37:47 -0500557 pdev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(devid),
558 devid & 0xff);
Joerg Roedel30bf2df2017-05-15 16:25:03 +0200559 if (pdev)
560 dev_data = get_dev_data(&pdev->dev);
561
562 if (dev_data && __ratelimit(&dev_data->rs)) {
Bjorn Helgaas5f226da2019-02-08 16:05:53 -0600563 pci_err(pdev, "Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x]\n",
Joerg Roedel30bf2df2017-05-15 16:25:03 +0200564 domain_id, address, flags);
565 } else if (printk_ratelimit()) {
Joerg Roedel6f5086a62018-11-27 17:18:52 +0100566 pr_err("Event logged [IO_PAGE_FAULT device=%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n",
Joerg Roedel30bf2df2017-05-15 16:25:03 +0200567 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
568 domain_id, address, flags);
569 }
570
571 if (pdev)
572 pci_dev_put(pdev);
573}
574
Joerg Roedela345b232009-09-03 15:01:43 +0200575static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200576{
Gary R Hook90ca3852018-03-08 18:34:41 -0600577 struct device *dev = iommu->iommu.dev;
Gary R Hooke7f63ff2018-05-01 14:53:00 -0500578 int type, devid, pasid, flags, tag;
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200579 volatile u32 *event = __evt;
580 int count = 0;
581 u64 address;
582
583retry:
584 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
585 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
Gary R Hookd64c0482018-05-01 14:52:52 -0500586 pasid = PPR_PASID(*(u64 *)&event[0]);
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200587 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
588 address = (u64)(((u64)event[3]) << 32) | event[2];
589
590 if (type == 0) {
591 /* Did we hit the erratum? */
592 if (++count == LOOP_TIMEOUT) {
Joerg Roedel101fa032018-11-27 16:22:31 +0100593 pr_err("No event written to event log\n");
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200594 return;
595 }
596 udelay(1);
597 goto retry;
598 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200599
Joerg Roedel30bf2df2017-05-15 16:25:03 +0200600 if (type == EVENT_TYPE_IO_FAULT) {
Gary R Hookd64c0482018-05-01 14:52:52 -0500601 amd_iommu_report_page_fault(devid, pasid, address, flags);
Joerg Roedel30bf2df2017-05-15 16:25:03 +0200602 return;
Joerg Roedel30bf2df2017-05-15 16:25:03 +0200603 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200604
605 switch (type) {
606 case EVENT_TYPE_ILL_DEV:
Joerg Roedel6f5086a62018-11-27 17:18:52 +0100607 dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
Gary R Hook90ca3852018-03-08 18:34:41 -0600608 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Gary R Hookd64c0482018-05-01 14:52:52 -0500609 pasid, address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200610 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200611 break;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200612 case EVENT_TYPE_DEV_TAB_ERR:
Joerg Roedel1a21ee12018-11-27 16:43:57 +0100613 dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
Joerg Roedel6f5086a62018-11-27 17:18:52 +0100614 "address=0x%llx flags=0x%04x]\n",
Gary R Hook90ca3852018-03-08 18:34:41 -0600615 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
616 address, flags);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200617 break;
618 case EVENT_TYPE_PAGE_TAB_ERR:
Joerg Roedel6f5086a62018-11-27 17:18:52 +0100619 dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n",
Gary R Hook90ca3852018-03-08 18:34:41 -0600620 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Gary R Hookd64c0482018-05-01 14:52:52 -0500621 pasid, address, flags);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200622 break;
623 case EVENT_TYPE_ILL_CMD:
Joerg Roedel6f5086a62018-11-27 17:18:52 +0100624 dev_err(dev, "Event logged [ILLEGAL_COMMAND_ERROR address=0x%llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200625 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200626 break;
627 case EVENT_TYPE_CMD_HARD_ERR:
Joerg Roedel6f5086a62018-11-27 17:18:52 +0100628 dev_err(dev, "Event logged [COMMAND_HARDWARE_ERROR address=0x%llx flags=0x%04x]\n",
Gary R Hookd64c0482018-05-01 14:52:52 -0500629 address, flags);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200630 break;
631 case EVENT_TYPE_IOTLB_INV_TO:
Joerg Roedel6f5086a62018-11-27 17:18:52 +0100632 dev_err(dev, "Event logged [IOTLB_INV_TIMEOUT device=%02x:%02x.%x address=0x%llx]\n",
Gary R Hook90ca3852018-03-08 18:34:41 -0600633 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
634 address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200635 break;
636 case EVENT_TYPE_INV_DEV_REQ:
Joerg Roedel6f5086a62018-11-27 17:18:52 +0100637 dev_err(dev, "Event logged [INVALID_DEVICE_REQUEST device=%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
Gary R Hook90ca3852018-03-08 18:34:41 -0600638 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Gary R Hookd64c0482018-05-01 14:52:52 -0500639 pasid, address, flags);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200640 break;
Gary R Hooke7f63ff2018-05-01 14:53:00 -0500641 case EVENT_TYPE_INV_PPR_REQ:
642 pasid = ((event[0] >> 16) & 0xFFFF)
643 | ((event[1] << 6) & 0xF0000);
644 tag = event[1] & 0x03FF;
YueHaibingc1ddcf1c2018-11-08 11:57:33 +0000645 dev_err(dev, "Event logged [INVALID_PPR_REQUEST device=%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x tag=0x%03x]\n",
Gary R Hooke7f63ff2018-05-01 14:53:00 -0500646 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
YueHaibingc1ddcf1c2018-11-08 11:57:33 +0000647 pasid, address, flags, tag);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200648 break;
649 default:
Joerg Roedel1a21ee12018-11-27 16:43:57 +0100650 dev_err(dev, "Event logged [UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n",
Gary R Hook90ca3852018-03-08 18:34:41 -0600651 event[0], event[1], event[2], event[3]);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200652 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200653
654 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200655}
656
657static void iommu_poll_events(struct amd_iommu *iommu)
658{
659 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200660
661 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
662 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
663
664 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200665 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200666 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200667 }
668
669 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200670}
671
Joerg Roedeleee53532012-06-01 15:20:23 +0200672static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100673{
674 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100675
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100676 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
Joerg Roedel101fa032018-11-27 16:22:31 +0100677 pr_err_ratelimited("Unknown PPR request received\n");
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100678 return;
679 }
680
681 fault.address = raw[1];
682 fault.pasid = PPR_PASID(raw[0]);
683 fault.device_id = PPR_DEVID(raw[0]);
684 fault.tag = PPR_TAG(raw[0]);
685 fault.flags = PPR_FLAGS(raw[0]);
686
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100687 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
688}
689
690static void iommu_poll_ppr_log(struct amd_iommu *iommu)
691{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100692 u32 head, tail;
693
694 if (iommu->ppr_log == NULL)
695 return;
696
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100697 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
698 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
699
700 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200701 volatile u64 *raw;
702 u64 entry[2];
703 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100704
Joerg Roedeleee53532012-06-01 15:20:23 +0200705 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100706
Joerg Roedeleee53532012-06-01 15:20:23 +0200707 /*
708 * Hardware bug: Interrupt may arrive before the entry is
709 * written to memory. If this happens we need to wait for the
710 * entry to arrive.
711 */
712 for (i = 0; i < LOOP_TIMEOUT; ++i) {
713 if (PPR_REQ_TYPE(raw[0]) != 0)
714 break;
715 udelay(1);
716 }
717
718 /* Avoid memcpy function-call overhead */
719 entry[0] = raw[0];
720 entry[1] = raw[1];
721
722 /*
723 * To detect the hardware bug we need to clear the entry
724 * back to zero.
725 */
726 raw[0] = raw[1] = 0UL;
727
728 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100729 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
730 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200731
Joerg Roedeleee53532012-06-01 15:20:23 +0200732 /* Handle PPR entry */
733 iommu_handle_ppr_entry(iommu, entry);
734
Joerg Roedeleee53532012-06-01 15:20:23 +0200735 /* Refresh ring-buffer information */
736 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100737 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
738 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100739}
740
Suravee Suthikulpanitbd6fcef2016-08-23 13:52:37 -0500741#ifdef CONFIG_IRQ_REMAP
742static int (*iommu_ga_log_notifier)(u32);
743
744int amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
745{
746 iommu_ga_log_notifier = notifier;
747
748 return 0;
749}
750EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier);
751
752static void iommu_poll_ga_log(struct amd_iommu *iommu)
753{
754 u32 head, tail, cnt = 0;
755
756 if (iommu->ga_log == NULL)
757 return;
758
759 head = readl(iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
760 tail = readl(iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
761
762 while (head != tail) {
763 volatile u64 *raw;
764 u64 log_entry;
765
766 raw = (u64 *)(iommu->ga_log + head);
767 cnt++;
768
769 /* Avoid memcpy function-call overhead */
770 log_entry = *raw;
771
772 /* Update head pointer of hardware ring-buffer */
773 head = (head + GA_ENTRY_SIZE) % GA_LOG_SIZE;
774 writel(head, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
775
776 /* Handle GA entry */
777 switch (GA_REQ_TYPE(log_entry)) {
778 case GA_GUEST_NR:
779 if (!iommu_ga_log_notifier)
780 break;
781
Joerg Roedel101fa032018-11-27 16:22:31 +0100782 pr_debug("%s: devid=%#x, ga_tag=%#x\n",
Suravee Suthikulpanitbd6fcef2016-08-23 13:52:37 -0500783 __func__, GA_DEVID(log_entry),
784 GA_TAG(log_entry));
785
786 if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0)
Joerg Roedel101fa032018-11-27 16:22:31 +0100787 pr_err("GA log notifier failed.\n");
Suravee Suthikulpanitbd6fcef2016-08-23 13:52:37 -0500788 break;
789 default:
790 break;
791 }
792 }
793}
794#endif /* CONFIG_IRQ_REMAP */
795
796#define AMD_IOMMU_INT_MASK \
797 (MMIO_STATUS_EVT_INT_MASK | \
798 MMIO_STATUS_PPR_INT_MASK | \
799 MMIO_STATUS_GALOG_INT_MASK)
800
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200801irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200802{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500803 struct amd_iommu *iommu = (struct amd_iommu *) data;
804 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200805
Suravee Suthikulpanitbd6fcef2016-08-23 13:52:37 -0500806 while (status & AMD_IOMMU_INT_MASK) {
807 /* Enable EVT and PPR and GA interrupts again */
808 writel(AMD_IOMMU_INT_MASK,
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500809 iommu->mmio_base + MMIO_STATUS_OFFSET);
810
811 if (status & MMIO_STATUS_EVT_INT_MASK) {
Joerg Roedel101fa032018-11-27 16:22:31 +0100812 pr_devel("Processing IOMMU Event Log\n");
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500813 iommu_poll_events(iommu);
814 }
815
816 if (status & MMIO_STATUS_PPR_INT_MASK) {
Joerg Roedel101fa032018-11-27 16:22:31 +0100817 pr_devel("Processing IOMMU PPR Log\n");
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500818 iommu_poll_ppr_log(iommu);
819 }
820
Suravee Suthikulpanitbd6fcef2016-08-23 13:52:37 -0500821#ifdef CONFIG_IRQ_REMAP
822 if (status & MMIO_STATUS_GALOG_INT_MASK) {
Joerg Roedel101fa032018-11-27 16:22:31 +0100823 pr_devel("Processing IOMMU GA Log\n");
Suravee Suthikulpanitbd6fcef2016-08-23 13:52:37 -0500824 iommu_poll_ga_log(iommu);
825 }
826#endif
827
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500828 /*
829 * Hardware bug: ERBT1312
830 * When re-enabling interrupt (by writing 1
831 * to clear the bit), the hardware might also try to set
832 * the interrupt bit in the event status register.
833 * In this scenario, the bit will be set, and disable
834 * subsequent interrupts.
835 *
836 * Workaround: The IOMMU driver should read back the
837 * status register and check if the interrupt bits are cleared.
838 * If not, driver will need to go through the interrupt handler
839 * again and re-clear the bits
840 */
841 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100842 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200843 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200844}
845
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200846irqreturn_t amd_iommu_int_handler(int irq, void *data)
847{
848 return IRQ_WAKE_THREAD;
849}
850
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200851/****************************************************************************
852 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200853 * IOMMU command queuing functions
854 *
855 ****************************************************************************/
856
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200857static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200858{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200859 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200860
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200861 while (*sem == 0 && i < LOOP_TIMEOUT) {
862 udelay(1);
863 i += 1;
864 }
865
866 if (i == LOOP_TIMEOUT) {
Joerg Roedel101fa032018-11-27 16:22:31 +0100867 pr_alert("Completion-Wait loop timed out\n");
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200868 return -EIO;
869 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200870
871 return 0;
872}
873
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200874static void copy_cmd_to_buffer(struct amd_iommu *iommu,
Tom Lendackyd334a562017-06-05 14:52:12 -0500875 struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200876{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200877 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200878
Tom Lendackyd334a562017-06-05 14:52:12 -0500879 target = iommu->cmd_buf + iommu->cmd_buf_tail;
880
881 iommu->cmd_buf_tail += sizeof(*cmd);
882 iommu->cmd_buf_tail %= CMD_BUFFER_SIZE;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200883
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200884 /* Copy command to buffer */
885 memcpy(target, cmd, sizeof(*cmd));
886
887 /* Tell the IOMMU about it */
Tom Lendackyd334a562017-06-05 14:52:12 -0500888 writel(iommu->cmd_buf_tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200889}
890
Joerg Roedel815b33f2011-04-06 17:26:49 +0200891static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200892{
Tom Lendacky2543a782017-07-17 16:10:24 -0500893 u64 paddr = iommu_virt_to_phys((void *)address);
894
Joerg Roedel815b33f2011-04-06 17:26:49 +0200895 WARN_ON(address & 0x7ULL);
896
Joerg Roedelded46732011-04-06 10:53:48 +0200897 memset(cmd, 0, sizeof(*cmd));
Tom Lendacky2543a782017-07-17 16:10:24 -0500898 cmd->data[0] = lower_32_bits(paddr) | CMD_COMPL_WAIT_STORE_MASK;
899 cmd->data[1] = upper_32_bits(paddr);
Joerg Roedel815b33f2011-04-06 17:26:49 +0200900 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200901 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
902}
903
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200904static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
905{
906 memset(cmd, 0, sizeof(*cmd));
907 cmd->data[0] = devid;
908 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
909}
910
Joerg Roedel11b64022011-04-06 11:49:28 +0200911static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
912 size_t size, u16 domid, int pde)
913{
914 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100915 bool s;
Joerg Roedel11b64022011-04-06 11:49:28 +0200916
917 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100918 s = false;
Joerg Roedel11b64022011-04-06 11:49:28 +0200919
920 if (pages > 1) {
921 /*
922 * If we have to flush more than one page, flush all
923 * TLB entries for this domain
924 */
925 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100926 s = true;
Joerg Roedel11b64022011-04-06 11:49:28 +0200927 }
928
929 address &= PAGE_MASK;
930
931 memset(cmd, 0, sizeof(*cmd));
932 cmd->data[1] |= domid;
933 cmd->data[2] = lower_32_bits(address);
934 cmd->data[3] = upper_32_bits(address);
935 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
936 if (s) /* size bit - we flush more than one 4kb page */
937 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200938 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200939 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
940}
941
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200942static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
943 u64 address, size_t size)
944{
945 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100946 bool s;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200947
948 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100949 s = false;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200950
951 if (pages > 1) {
952 /*
953 * If we have to flush more than one page, flush all
954 * TLB entries for this domain
955 */
956 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100957 s = true;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200958 }
959
960 address &= PAGE_MASK;
961
962 memset(cmd, 0, sizeof(*cmd));
963 cmd->data[0] = devid;
964 cmd->data[0] |= (qdep & 0xff) << 24;
965 cmd->data[1] = devid;
966 cmd->data[2] = lower_32_bits(address);
967 cmd->data[3] = upper_32_bits(address);
968 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
969 if (s)
970 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
971}
972
Joerg Roedel22e266c2011-11-21 15:59:08 +0100973static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
974 u64 address, bool size)
975{
976 memset(cmd, 0, sizeof(*cmd));
977
978 address &= ~(0xfffULL);
979
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600980 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100981 cmd->data[1] = domid;
982 cmd->data[2] = lower_32_bits(address);
983 cmd->data[3] = upper_32_bits(address);
984 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
985 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
986 if (size)
987 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
988 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
989}
990
991static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
992 int qdep, u64 address, bool size)
993{
994 memset(cmd, 0, sizeof(*cmd));
995
996 address &= ~(0xfffULL);
997
998 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600999 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +01001000 cmd->data[0] |= (qdep & 0xff) << 24;
1001 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -06001002 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +01001003 cmd->data[2] = lower_32_bits(address);
1004 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
1005 cmd->data[3] = upper_32_bits(address);
1006 if (size)
1007 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
1008 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
1009}
1010
Joerg Roedelc99afa22011-11-21 18:19:25 +01001011static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
1012 int status, int tag, bool gn)
1013{
1014 memset(cmd, 0, sizeof(*cmd));
1015
1016 cmd->data[0] = devid;
1017 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001018 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +01001019 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
1020 }
1021 cmd->data[3] = tag & 0x1ff;
1022 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
1023
1024 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
1025}
1026
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001027static void build_inv_all(struct iommu_cmd *cmd)
1028{
1029 memset(cmd, 0, sizeof(*cmd));
1030 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001031}
1032
Joerg Roedel7ef27982012-06-21 16:46:04 +02001033static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
1034{
1035 memset(cmd, 0, sizeof(*cmd));
1036 cmd->data[0] = devid;
1037 CMD_SET_TYPE(cmd, CMD_INV_IRT);
1038}
1039
Joerg Roedel431b2a22008-07-11 17:14:22 +02001040/*
Joerg Roedelb6c02712008-06-26 21:27:53 +02001041 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001042 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001043 */
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001044static int __iommu_queue_command_sync(struct amd_iommu *iommu,
1045 struct iommu_cmd *cmd,
1046 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001047{
Tom Lendacky23e967e2017-06-05 14:52:26 -05001048 unsigned int count = 0;
Tom Lendackyd334a562017-06-05 14:52:12 -05001049 u32 left, next_tail;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001050
Tom Lendackyd334a562017-06-05 14:52:12 -05001051 next_tail = (iommu->cmd_buf_tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001052again:
Tom Lendackyd334a562017-06-05 14:52:12 -05001053 left = (iommu->cmd_buf_head - next_tail) % CMD_BUFFER_SIZE;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001054
Huang Rui432abf62016-12-12 07:28:26 -05001055 if (left <= 0x20) {
Tom Lendacky23e967e2017-06-05 14:52:26 -05001056 /* Skip udelay() the first time around */
1057 if (count++) {
1058 if (count == LOOP_TIMEOUT) {
Joerg Roedel101fa032018-11-27 16:22:31 +01001059 pr_err("Command buffer timeout\n");
Tom Lendacky23e967e2017-06-05 14:52:26 -05001060 return -EIO;
1061 }
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001062
Tom Lendacky23e967e2017-06-05 14:52:26 -05001063 udelay(1);
Tom Lendackyd334a562017-06-05 14:52:12 -05001064 }
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001065
Tom Lendacky23e967e2017-06-05 14:52:26 -05001066 /* Update head and recheck remaining space */
1067 iommu->cmd_buf_head = readl(iommu->mmio_base +
1068 MMIO_CMD_HEAD_OFFSET);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001069
1070 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +02001071 }
1072
Tom Lendackyd334a562017-06-05 14:52:12 -05001073 copy_cmd_to_buffer(iommu, cmd);
Joerg Roedel519c31b2008-08-14 19:55:15 +02001074
Tom Lendacky23e967e2017-06-05 14:52:26 -05001075 /* Do we need to make sure all commands are processed? */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001076 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001077
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001078 return 0;
1079}
1080
1081static int iommu_queue_command_sync(struct amd_iommu *iommu,
1082 struct iommu_cmd *cmd,
1083 bool sync)
1084{
1085 unsigned long flags;
1086 int ret;
1087
Scott Wood27790392018-01-21 03:28:54 -06001088 raw_spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001089 ret = __iommu_queue_command_sync(iommu, cmd, sync);
Scott Wood27790392018-01-21 03:28:54 -06001090 raw_spin_unlock_irqrestore(&iommu->lock, flags);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001091
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001092 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001093}
1094
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001095static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1096{
1097 return iommu_queue_command_sync(iommu, cmd, true);
1098}
1099
Joerg Roedel8d201962008-12-02 20:34:41 +01001100/*
1101 * This function queues a completion wait command into the command
1102 * buffer of an IOMMU
1103 */
Joerg Roedel8d201962008-12-02 20:34:41 +01001104static int iommu_completion_wait(struct amd_iommu *iommu)
1105{
Joerg Roedel815b33f2011-04-06 17:26:49 +02001106 struct iommu_cmd cmd;
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001107 unsigned long flags;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001108 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001109
1110 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001111 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001112
Joerg Roedel8d201962008-12-02 20:34:41 +01001113
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001114 build_completion_wait(&cmd, (u64)&iommu->cmd_sem);
1115
Scott Wood27790392018-01-21 03:28:54 -06001116 raw_spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001117
1118 iommu->cmd_sem = 0;
1119
1120 ret = __iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +01001121 if (ret)
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001122 goto out_unlock;
Joerg Roedel8d201962008-12-02 20:34:41 +01001123
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001124 ret = wait_on_sem(&iommu->cmd_sem);
1125
1126out_unlock:
Scott Wood27790392018-01-21 03:28:54 -06001127 raw_spin_unlock_irqrestore(&iommu->lock, flags);
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001128
1129 return ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001130}
1131
Joerg Roedeld8c13082011-04-06 18:51:26 +02001132static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001133{
1134 struct iommu_cmd cmd;
1135
Joerg Roedeld8c13082011-04-06 18:51:26 +02001136 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001137
Joerg Roedeld8c13082011-04-06 18:51:26 +02001138 return iommu_queue_command(iommu, &cmd);
1139}
1140
Joerg Roedel0688a092017-08-23 15:50:03 +02001141static void amd_iommu_flush_dte_all(struct amd_iommu *iommu)
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001142{
1143 u32 devid;
1144
1145 for (devid = 0; devid <= 0xffff; ++devid)
1146 iommu_flush_dte(iommu, devid);
1147
1148 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001149}
1150
1151/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001152 * This function uses heavy locking and may disable irqs for some time. But
1153 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001154 */
Joerg Roedel0688a092017-08-23 15:50:03 +02001155static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001156{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001157 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001158
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001159 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1160 struct iommu_cmd cmd;
1161 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1162 dom_id, 1);
1163 iommu_queue_command(iommu, &cmd);
1164 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001165
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001166 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001167}
1168
Stuart Hayes36b72002019-09-05 12:09:48 -05001169static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id)
1170{
1171 struct iommu_cmd cmd;
1172
1173 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1174 dom_id, 1);
1175 iommu_queue_command(iommu, &cmd);
1176
1177 iommu_completion_wait(iommu);
1178}
1179
Joerg Roedel0688a092017-08-23 15:50:03 +02001180static void amd_iommu_flush_all(struct amd_iommu *iommu)
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001181{
1182 struct iommu_cmd cmd;
1183
1184 build_inv_all(&cmd);
1185
1186 iommu_queue_command(iommu, &cmd);
1187 iommu_completion_wait(iommu);
1188}
1189
Joerg Roedel7ef27982012-06-21 16:46:04 +02001190static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1191{
1192 struct iommu_cmd cmd;
1193
1194 build_inv_irt(&cmd, devid);
1195
1196 iommu_queue_command(iommu, &cmd);
1197}
1198
Joerg Roedel0688a092017-08-23 15:50:03 +02001199static void amd_iommu_flush_irt_all(struct amd_iommu *iommu)
Joerg Roedel7ef27982012-06-21 16:46:04 +02001200{
1201 u32 devid;
1202
1203 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1204 iommu_flush_irt(iommu, devid);
1205
1206 iommu_completion_wait(iommu);
1207}
1208
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001209void iommu_flush_all_caches(struct amd_iommu *iommu)
1210{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001211 if (iommu_feature(iommu, FEATURE_IA)) {
Joerg Roedel0688a092017-08-23 15:50:03 +02001212 amd_iommu_flush_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001213 } else {
Joerg Roedel0688a092017-08-23 15:50:03 +02001214 amd_iommu_flush_dte_all(iommu);
1215 amd_iommu_flush_irt_all(iommu);
1216 amd_iommu_flush_tlb_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001217 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001218}
1219
Joerg Roedel431b2a22008-07-11 17:14:22 +02001220/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001221 * Command send function for flushing on-device TLB
1222 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001223static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1224 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001225{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001226 struct amd_iommu *iommu;
1227 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001228 int qdep;
1229
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001230 qdep = dev_data->ats.qdep;
1231 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001232
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001233 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001234
1235 return iommu_queue_command(iommu, &cmd);
1236}
1237
1238/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001239 * Command send function for invalidating a device table entry
1240 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001241static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001242{
1243 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001244 u16 alias;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001245 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001246
Joerg Roedel6c542042011-06-09 17:07:31 +02001247 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001248 alias = dev_data->alias;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001249
Joerg Roedelf62dda62011-06-09 12:55:35 +02001250 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001251 if (!ret && alias != dev_data->devid)
1252 ret = iommu_flush_dte(iommu, alias);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001253 if (ret)
1254 return ret;
1255
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001256 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001257 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001258
1259 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001260}
1261
Joerg Roedel431b2a22008-07-11 17:14:22 +02001262/*
1263 * TLB invalidation function which is called from the mapping functions.
1264 * It invalidates a single PTE if the range to flush is within a single
1265 * page. Otherwise it flushes the whole TLB of the IOMMU.
1266 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001267static void __domain_flush_pages(struct protection_domain *domain,
1268 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001269{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001270 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001271 struct iommu_cmd cmd;
1272 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001273
Joerg Roedel11b64022011-04-06 11:49:28 +02001274 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001275
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -06001276 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001277 if (!domain->dev_iommu[i])
1278 continue;
1279
1280 /*
1281 * Devices of this domain are behind this IOMMU
1282 * We need a TLB flush
1283 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001284 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001285 }
1286
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001287 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001288
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001289 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001290 continue;
1291
Joerg Roedel6c542042011-06-09 17:07:31 +02001292 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001293 }
1294
Joerg Roedel11b64022011-04-06 11:49:28 +02001295 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001296}
1297
Joerg Roedel17b124b2011-04-06 18:01:35 +02001298static void domain_flush_pages(struct protection_domain *domain,
1299 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001300{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001301 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001302}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001303
Joerg Roedel1c655772008-09-04 18:40:05 +02001304/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001305static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001306{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001307 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001308}
1309
Chris Wright42a49f92009-06-15 15:42:00 +02001310/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001311static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001312{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001313 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1314}
1315
1316static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001317{
1318 int i;
1319
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -06001320 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
Joerg Roedelf1eae7c2016-07-06 12:50:35 +02001321 if (domain && !domain->dev_iommu[i])
Joerg Roedelb6c02712008-06-26 21:27:53 +02001322 continue;
1323
1324 /*
1325 * Devices of this domain are behind this IOMMU
1326 * We need to wait for completion of all commands.
1327 */
1328 iommu_completion_wait(amd_iommus[i]);
1329 }
1330}
1331
Tom Murphy5cd3f2e2019-06-13 23:04:55 +01001332/* Flush the not present cache if it exists */
1333static void domain_flush_np_cache(struct protection_domain *domain,
1334 dma_addr_t iova, size_t size)
1335{
1336 if (unlikely(amd_iommu_np_cache)) {
1337 domain_flush_pages(domain, iova, size);
1338 domain_flush_complete(domain);
1339 }
1340}
1341
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001342
Joerg Roedel43f49602008-12-02 21:01:12 +01001343/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001344 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001345 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001346static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001347{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001348 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001349
1350 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001351 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001352}
1353
Joerg Roedel431b2a22008-07-11 17:14:22 +02001354/****************************************************************************
1355 *
1356 * The functions below are used the create the page table mappings for
1357 * unity mapped regions.
1358 *
1359 ****************************************************************************/
1360
Joerg Roedelac3a7092018-11-09 12:07:06 +01001361static void free_page_list(struct page *freelist)
1362{
1363 while (freelist != NULL) {
1364 unsigned long p = (unsigned long)page_address(freelist);
1365 freelist = freelist->freelist;
1366 free_page(p);
1367 }
1368}
1369
1370static struct page *free_pt_page(unsigned long pt, struct page *freelist)
1371{
1372 struct page *p = virt_to_page((void *)pt);
1373
1374 p->freelist = freelist;
1375
1376 return p;
1377}
1378
1379#define DEFINE_FREE_PT_FN(LVL, FN) \
1380static struct page *free_pt_##LVL (unsigned long __pt, struct page *freelist) \
1381{ \
1382 unsigned long p; \
1383 u64 *pt; \
1384 int i; \
1385 \
1386 pt = (u64 *)__pt; \
1387 \
1388 for (i = 0; i < 512; ++i) { \
1389 /* PTE present? */ \
1390 if (!IOMMU_PTE_PRESENT(pt[i])) \
1391 continue; \
1392 \
1393 /* Large PTE? */ \
1394 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1395 PM_PTE_LEVEL(pt[i]) == 7) \
1396 continue; \
1397 \
1398 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1399 freelist = FN(p, freelist); \
1400 } \
1401 \
1402 return free_pt_page((unsigned long)pt, freelist); \
1403}
1404
1405DEFINE_FREE_PT_FN(l2, free_pt_page)
1406DEFINE_FREE_PT_FN(l3, free_pt_l2)
1407DEFINE_FREE_PT_FN(l4, free_pt_l3)
1408DEFINE_FREE_PT_FN(l5, free_pt_l4)
1409DEFINE_FREE_PT_FN(l6, free_pt_l5)
1410
Joerg Roedel409afa42018-11-09 12:07:07 +01001411static struct page *free_sub_pt(unsigned long root, int mode,
1412 struct page *freelist)
Joerg Roedelac3a7092018-11-09 12:07:06 +01001413{
Joerg Roedel409afa42018-11-09 12:07:07 +01001414 switch (mode) {
Joerg Roedelac3a7092018-11-09 12:07:06 +01001415 case PAGE_MODE_NONE:
Joerg Roedel69be8852018-11-09 12:07:08 +01001416 case PAGE_MODE_7_LEVEL:
Joerg Roedelac3a7092018-11-09 12:07:06 +01001417 break;
1418 case PAGE_MODE_1_LEVEL:
1419 freelist = free_pt_page(root, freelist);
1420 break;
1421 case PAGE_MODE_2_LEVEL:
1422 freelist = free_pt_l2(root, freelist);
1423 break;
1424 case PAGE_MODE_3_LEVEL:
1425 freelist = free_pt_l3(root, freelist);
1426 break;
1427 case PAGE_MODE_4_LEVEL:
1428 freelist = free_pt_l4(root, freelist);
1429 break;
1430 case PAGE_MODE_5_LEVEL:
1431 freelist = free_pt_l5(root, freelist);
1432 break;
1433 case PAGE_MODE_6_LEVEL:
1434 freelist = free_pt_l6(root, freelist);
1435 break;
1436 default:
1437 BUG();
1438 }
1439
Joerg Roedel409afa42018-11-09 12:07:07 +01001440 return freelist;
1441}
1442
1443static void free_pagetable(struct protection_domain *domain)
1444{
1445 unsigned long root = (unsigned long)domain->pt_root;
1446 struct page *freelist = NULL;
1447
Joerg Roedel69be8852018-11-09 12:07:08 +01001448 BUG_ON(domain->mode < PAGE_MODE_NONE ||
1449 domain->mode > PAGE_MODE_6_LEVEL);
1450
Andrei Dulea34c09892019-09-13 16:42:28 +02001451 freelist = free_sub_pt(root, domain->mode, freelist);
Joerg Roedel409afa42018-11-09 12:07:07 +01001452
Joerg Roedelac3a7092018-11-09 12:07:06 +01001453 free_page_list(freelist);
1454}
1455
Joerg Roedel431b2a22008-07-11 17:14:22 +02001456/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001457 * This function is used to add another level to an IO page table. Adding
1458 * another level increases the size of the address space by 9 bits to a size up
1459 * to 64 bits.
1460 */
Joerg Roedel754265b2019-09-06 10:39:54 +02001461static void increase_address_space(struct protection_domain *domain,
Joerg Roedel308973d2009-11-24 17:43:32 +01001462 gfp_t gfp)
1463{
Joerg Roedel754265b2019-09-06 10:39:54 +02001464 unsigned long flags;
Joerg Roedel308973d2009-11-24 17:43:32 +01001465 u64 *pte;
1466
Joerg Roedel754265b2019-09-06 10:39:54 +02001467 spin_lock_irqsave(&domain->lock, flags);
1468
1469 if (WARN_ON_ONCE(domain->mode == PAGE_MODE_6_LEVEL))
Joerg Roedel308973d2009-11-24 17:43:32 +01001470 /* address space already 64 bit large */
Joerg Roedel754265b2019-09-06 10:39:54 +02001471 goto out;
Joerg Roedel308973d2009-11-24 17:43:32 +01001472
1473 pte = (void *)get_zeroed_page(gfp);
1474 if (!pte)
Joerg Roedel754265b2019-09-06 10:39:54 +02001475 goto out;
Joerg Roedel308973d2009-11-24 17:43:32 +01001476
1477 *pte = PM_LEVEL_PDE(domain->mode,
Tom Lendacky2543a782017-07-17 16:10:24 -05001478 iommu_virt_to_phys(domain->pt_root));
Joerg Roedel308973d2009-11-24 17:43:32 +01001479 domain->pt_root = pte;
1480 domain->mode += 1;
1481 domain->updated = true;
1482
Joerg Roedel754265b2019-09-06 10:39:54 +02001483out:
1484 spin_unlock_irqrestore(&domain->lock, flags);
1485
1486 return;
Joerg Roedel308973d2009-11-24 17:43:32 +01001487}
1488
1489static u64 *alloc_pte(struct protection_domain *domain,
1490 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001491 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001492 u64 **pte_page,
1493 gfp_t gfp)
1494{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001495 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001496 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001497
1498 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001499
1500 while (address > PM_LEVEL_SIZE(domain->mode))
1501 increase_address_space(domain, gfp);
1502
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001503 level = domain->mode - 1;
1504 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1505 address = PAGE_SIZE_ALIGN(address, page_size);
1506 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001507
1508 while (level > end_lvl) {
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001509 u64 __pte, __npte;
Joerg Roedel6d568ef2018-11-09 12:07:09 +01001510 int pte_level;
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001511
Joerg Roedel6d568ef2018-11-09 12:07:09 +01001512 __pte = *pte;
1513 pte_level = PM_PTE_LEVEL(__pte);
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001514
Joerg Roedel6d568ef2018-11-09 12:07:09 +01001515 if (!IOMMU_PTE_PRESENT(__pte) ||
Andrei Dulea6ccb72f2019-09-13 16:42:29 +02001516 pte_level == PAGE_MODE_NONE ||
Joerg Roedel6d568ef2018-11-09 12:07:09 +01001517 pte_level == PAGE_MODE_7_LEVEL) {
Joerg Roedel308973d2009-11-24 17:43:32 +01001518 page = (u64 *)get_zeroed_page(gfp);
1519 if (!page)
1520 return NULL;
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001521
Tom Lendacky2543a782017-07-17 16:10:24 -05001522 __npte = PM_LEVEL_PDE(level, iommu_virt_to_phys(page));
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001523
Baoquan He134414f2016-09-15 16:50:50 +08001524 /* pte could have been changed somewhere. */
Joerg Roedel9db034d2018-11-09 12:07:10 +01001525 if (cmpxchg64(pte, __pte, __npte) != __pte)
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001526 free_page((unsigned long)page);
Andrei Dulea6ccb72f2019-09-13 16:42:29 +02001527 else if (IOMMU_PTE_PRESENT(__pte))
Joerg Roedel6d568ef2018-11-09 12:07:09 +01001528 domain->updated = true;
Joerg Roedel9db034d2018-11-09 12:07:10 +01001529
1530 continue;
Joerg Roedel308973d2009-11-24 17:43:32 +01001531 }
1532
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001533 /* No level skipping support yet */
Joerg Roedel6d568ef2018-11-09 12:07:09 +01001534 if (pte_level != level)
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001535 return NULL;
1536
Joerg Roedel308973d2009-11-24 17:43:32 +01001537 level -= 1;
1538
Joerg Roedel9db034d2018-11-09 12:07:10 +01001539 pte = IOMMU_PTE_PAGE(__pte);
Joerg Roedel308973d2009-11-24 17:43:32 +01001540
1541 if (pte_page && level == end_lvl)
1542 *pte_page = pte;
1543
1544 pte = &pte[PM_LEVEL_INDEX(level, address)];
1545 }
1546
1547 return pte;
1548}
1549
1550/*
1551 * This function checks if there is a PTE for a given dma address. If
1552 * there is one, it returns the pointer to it.
1553 */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001554static u64 *fetch_pte(struct protection_domain *domain,
1555 unsigned long address,
1556 unsigned long *page_size)
Joerg Roedel308973d2009-11-24 17:43:32 +01001557{
1558 int level;
1559 u64 *pte;
1560
yzhai003@ucr.edu46746862018-06-01 11:30:14 -07001561 *page_size = 0;
1562
Joerg Roedel24cd7722010-01-19 17:27:39 +01001563 if (address > PM_LEVEL_SIZE(domain->mode))
1564 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001565
Joerg Roedel3039ca12015-04-01 14:58:48 +02001566 level = domain->mode - 1;
1567 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1568 *page_size = PTE_LEVEL_PAGE_SIZE(level);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001569
1570 while (level > 0) {
1571
1572 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001573 if (!IOMMU_PTE_PRESENT(*pte))
1574 return NULL;
1575
Joerg Roedel24cd7722010-01-19 17:27:39 +01001576 /* Large PTE */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001577 if (PM_PTE_LEVEL(*pte) == 7 ||
1578 PM_PTE_LEVEL(*pte) == 0)
1579 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001580
1581 /* No level skipping support yet */
1582 if (PM_PTE_LEVEL(*pte) != level)
1583 return NULL;
1584
Joerg Roedel308973d2009-11-24 17:43:32 +01001585 level -= 1;
1586
Joerg Roedel24cd7722010-01-19 17:27:39 +01001587 /* Walk to the next level */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001588 pte = IOMMU_PTE_PAGE(*pte);
1589 pte = &pte[PM_LEVEL_INDEX(level, address)];
1590 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1591 }
1592
Andrei Dulea7f1f1682019-09-13 16:42:30 +02001593 /*
1594 * If we have a series of large PTEs, make
1595 * sure to return a pointer to the first one.
1596 */
1597 if (PM_PTE_LEVEL(*pte) == PAGE_MODE_7_LEVEL)
1598 pte = first_pte_l7(pte, page_size, NULL);
Joerg Roedel308973d2009-11-24 17:43:32 +01001599
1600 return pte;
1601}
1602
Joerg Roedel6f820bb2018-11-09 12:07:11 +01001603static struct page *free_clear_pte(u64 *pte, u64 pteval, struct page *freelist)
1604{
1605 unsigned long pt;
1606 int mode;
1607
1608 while (cmpxchg64(pte, pteval, 0) != pteval) {
1609 pr_warn("AMD-Vi: IOMMU pte changed since we read it\n");
1610 pteval = *pte;
1611 }
1612
1613 if (!IOMMU_PTE_PRESENT(pteval))
1614 return freelist;
1615
1616 pt = (unsigned long)IOMMU_PTE_PAGE(pteval);
1617 mode = IOMMU_PTE_MODE(pteval);
1618
1619 return free_sub_pt(pt, mode, freelist);
1620}
1621
Joerg Roedel308973d2009-11-24 17:43:32 +01001622/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001623 * Generic mapping functions. It maps a physical address into a DMA
1624 * address space. It allocates the page table pages if necessary.
1625 * In the future it can be extended to a generic mapping function
1626 * supporting all features of AMD IOMMU page tables like level skipping
1627 * and full 64 bit address spaces.
1628 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001629static int iommu_map_page(struct protection_domain *dom,
1630 unsigned long bus_addr,
1631 unsigned long phys_addr,
Joerg Roedelb911b892016-07-05 14:29:11 +02001632 unsigned long page_size,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001633 int prot,
Joerg Roedelb911b892016-07-05 14:29:11 +02001634 gfp_t gfp)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001635{
Joerg Roedel6f820bb2018-11-09 12:07:11 +01001636 struct page *freelist = NULL;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001637 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001638 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001639
Joerg Roedeld4b03662015-04-01 14:58:52 +02001640 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1641 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1642
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001643 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001644 return -EINVAL;
1645
Joerg Roedeld4b03662015-04-01 14:58:52 +02001646 count = PAGE_SIZE_PTE_COUNT(page_size);
Joerg Roedelb911b892016-07-05 14:29:11 +02001647 pte = alloc_pte(dom, bus_addr, page_size, NULL, gfp);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001648
Maurizio Lombardi63eaa752014-09-11 12:28:03 +02001649 if (!pte)
1650 return -ENOMEM;
1651
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001652 for (i = 0; i < count; ++i)
Joerg Roedel6f820bb2018-11-09 12:07:11 +01001653 freelist = free_clear_pte(&pte[i], pte[i], freelist);
1654
1655 if (freelist != NULL)
1656 dom->updated = true;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001657
Joerg Roedeld4b03662015-04-01 14:58:52 +02001658 if (count > 1) {
Tom Lendacky2543a782017-07-17 16:10:24 -05001659 __pte = PAGE_SIZE_PTE(__sme_set(phys_addr), page_size);
Baoquan He07a80a62017-08-09 16:33:36 +08001660 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_PR | IOMMU_PTE_FC;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001661 } else
Linus Torvalds4dfc2782017-09-09 15:03:24 -07001662 __pte = __sme_set(phys_addr) | IOMMU_PTE_PR | IOMMU_PTE_FC;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001663
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001664 if (prot & IOMMU_PROT_IR)
1665 __pte |= IOMMU_PTE_IR;
1666 if (prot & IOMMU_PROT_IW)
1667 __pte |= IOMMU_PTE_IW;
1668
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001669 for (i = 0; i < count; ++i)
1670 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001671
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001672 update_domain(dom);
1673
Joerg Roedel6f820bb2018-11-09 12:07:11 +01001674 /* Everything flushed out, free pages now */
1675 free_page_list(freelist);
1676
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001677 return 0;
1678}
1679
Joerg Roedel24cd7722010-01-19 17:27:39 +01001680static unsigned long iommu_unmap_page(struct protection_domain *dom,
1681 unsigned long bus_addr,
1682 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001683{
Joerg Roedel71b390e2015-04-01 14:58:49 +02001684 unsigned long long unmapped;
1685 unsigned long unmap_size;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001686 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001687
Joerg Roedel24cd7722010-01-19 17:27:39 +01001688 BUG_ON(!is_power_of_2(page_size));
1689
1690 unmapped = 0;
1691
1692 while (unmapped < page_size) {
1693
Joerg Roedel71b390e2015-04-01 14:58:49 +02001694 pte = fetch_pte(dom, bus_addr, &unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001695
Joerg Roedel71b390e2015-04-01 14:58:49 +02001696 if (pte) {
1697 int i, count;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001698
Joerg Roedel71b390e2015-04-01 14:58:49 +02001699 count = PAGE_SIZE_PTE_COUNT(unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001700 for (i = 0; i < count; i++)
1701 pte[i] = 0ULL;
1702 }
1703
1704 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1705 unmapped += unmap_size;
1706 }
1707
Alex Williamson60d0ca32013-06-21 14:33:19 -06001708 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001709
1710 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001711}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001712
Joerg Roedel431b2a22008-07-11 17:14:22 +02001713/****************************************************************************
1714 *
1715 * The next functions belong to the address allocator for the dma_ops
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001716 * interface functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001717 *
1718 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001719
Joerg Roedel9cabe892009-05-18 16:38:55 +02001720
Joerg Roedel256e4622016-07-05 14:23:01 +02001721static unsigned long dma_ops_alloc_iova(struct device *dev,
1722 struct dma_ops_domain *dma_dom,
1723 unsigned int pages, u64 dma_mask)
Joerg Roedela0f51442015-12-21 16:20:09 +01001724{
Joerg Roedel256e4622016-07-05 14:23:01 +02001725 unsigned long pfn = 0;
Joerg Roedela0f51442015-12-21 16:20:09 +01001726
Joerg Roedel256e4622016-07-05 14:23:01 +02001727 pages = __roundup_pow_of_two(pages);
Joerg Roedela0f51442015-12-21 16:20:09 +01001728
Joerg Roedel256e4622016-07-05 14:23:01 +02001729 if (dma_mask > DMA_BIT_MASK(32))
1730 pfn = alloc_iova_fast(&dma_dom->iovad, pages,
Tomasz Nowicki538d5b32017-09-20 10:52:02 +02001731 IOVA_PFN(DMA_BIT_MASK(32)), false);
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001732
Joerg Roedel256e4622016-07-05 14:23:01 +02001733 if (!pfn)
Tomasz Nowicki538d5b32017-09-20 10:52:02 +02001734 pfn = alloc_iova_fast(&dma_dom->iovad, pages,
1735 IOVA_PFN(dma_mask), true);
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001736
Joerg Roedel256e4622016-07-05 14:23:01 +02001737 return (pfn << PAGE_SHIFT);
Joerg Roedela0f51442015-12-21 16:20:09 +01001738}
1739
Joerg Roedel256e4622016-07-05 14:23:01 +02001740static void dma_ops_free_iova(struct dma_ops_domain *dma_dom,
1741 unsigned long address,
1742 unsigned int pages)
Joerg Roedel384de722009-05-15 12:30:05 +02001743{
Joerg Roedel256e4622016-07-05 14:23:01 +02001744 pages = __roundup_pow_of_two(pages);
1745 address >>= PAGE_SHIFT;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001746
Joerg Roedel256e4622016-07-05 14:23:01 +02001747 free_iova_fast(&dma_dom->iovad, address, pages);
Joerg Roedeld3086442008-06-26 21:27:57 +02001748}
1749
Joerg Roedel431b2a22008-07-11 17:14:22 +02001750/****************************************************************************
1751 *
1752 * The next functions belong to the domain allocation. A domain is
1753 * allocated for every IOMMU as the default domain. If device isolation
1754 * is enabled, every device get its own domain. The most important thing
1755 * about domains is the page table mapping the DMA address space they
1756 * contain.
1757 *
1758 ****************************************************************************/
1759
Joerg Roedelec487d12008-06-26 21:27:58 +02001760static u16 domain_id_alloc(void)
1761{
Joerg Roedelec487d12008-06-26 21:27:58 +02001762 int id;
1763
Sebastian Andrzej Siewior2bc00182018-03-22 16:22:35 +01001764 spin_lock(&pd_bitmap_lock);
Joerg Roedelec487d12008-06-26 21:27:58 +02001765 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1766 BUG_ON(id == 0);
1767 if (id > 0 && id < MAX_DOMAIN_ID)
1768 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1769 else
1770 id = 0;
Sebastian Andrzej Siewior2bc00182018-03-22 16:22:35 +01001771 spin_unlock(&pd_bitmap_lock);
Joerg Roedelec487d12008-06-26 21:27:58 +02001772
1773 return id;
1774}
1775
Joerg Roedela2acfb72008-12-02 18:28:53 +01001776static void domain_id_free(int id)
1777{
Sebastian Andrzej Siewior2bc00182018-03-22 16:22:35 +01001778 spin_lock(&pd_bitmap_lock);
Joerg Roedela2acfb72008-12-02 18:28:53 +01001779 if (id > 0 && id < MAX_DOMAIN_ID)
1780 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
Sebastian Andrzej Siewior2bc00182018-03-22 16:22:35 +01001781 spin_unlock(&pd_bitmap_lock);
Joerg Roedela2acfb72008-12-02 18:28:53 +01001782}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001783
Joerg Roedelb16137b2011-11-21 16:50:23 +01001784static void free_gcr3_tbl_level1(u64 *tbl)
1785{
1786 u64 *ptr;
1787 int i;
1788
1789 for (i = 0; i < 512; ++i) {
1790 if (!(tbl[i] & GCR3_VALID))
1791 continue;
1792
Tom Lendacky2543a782017-07-17 16:10:24 -05001793 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
Joerg Roedelb16137b2011-11-21 16:50:23 +01001794
1795 free_page((unsigned long)ptr);
1796 }
1797}
1798
1799static void free_gcr3_tbl_level2(u64 *tbl)
1800{
1801 u64 *ptr;
1802 int i;
1803
1804 for (i = 0; i < 512; ++i) {
1805 if (!(tbl[i] & GCR3_VALID))
1806 continue;
1807
Tom Lendacky2543a782017-07-17 16:10:24 -05001808 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
Joerg Roedelb16137b2011-11-21 16:50:23 +01001809
1810 free_gcr3_tbl_level1(ptr);
1811 }
1812}
1813
Joerg Roedel52815b72011-11-17 17:24:28 +01001814static void free_gcr3_table(struct protection_domain *domain)
1815{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001816 if (domain->glx == 2)
1817 free_gcr3_tbl_level2(domain->gcr3_tbl);
1818 else if (domain->glx == 1)
1819 free_gcr3_tbl_level1(domain->gcr3_tbl);
Joerg Roedel23d3a982015-08-13 11:15:13 +02001820 else
1821 BUG_ON(domain->glx != 0);
Joerg Roedelb16137b2011-11-21 16:50:23 +01001822
Joerg Roedel52815b72011-11-17 17:24:28 +01001823 free_page((unsigned long)domain->gcr3_tbl);
1824}
1825
Joerg Roedelfca6af62017-06-02 18:13:37 +02001826static void dma_ops_domain_flush_tlb(struct dma_ops_domain *dom)
1827{
Joerg Roedelfca6af62017-06-02 18:13:37 +02001828 domain_flush_tlb(&dom->domain);
1829 domain_flush_complete(&dom->domain);
Joerg Roedelfca6af62017-06-02 18:13:37 +02001830}
1831
Joerg Roedel9003d612017-08-10 17:19:13 +02001832static void iova_domain_flush_tlb(struct iova_domain *iovad)
Joerg Roedelfd621902017-06-02 15:37:26 +02001833{
Joerg Roedel9003d612017-08-10 17:19:13 +02001834 struct dma_ops_domain *dom;
Joerg Roedele241f8e762017-06-02 15:44:57 +02001835
Joerg Roedel9003d612017-08-10 17:19:13 +02001836 dom = container_of(iovad, struct dma_ops_domain, iovad);
Joerg Roedelfca6af62017-06-02 18:13:37 +02001837
1838 dma_ops_domain_flush_tlb(dom);
Joerg Roedelfca6af62017-06-02 18:13:37 +02001839}
1840
Joerg Roedel431b2a22008-07-11 17:14:22 +02001841/*
1842 * Free a domain, only used if something went wrong in the
1843 * allocation path and we need to free an already allocated page table
1844 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001845static void dma_ops_domain_free(struct dma_ops_domain *dom)
1846{
1847 if (!dom)
1848 return;
1849
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001850 put_iova_domain(&dom->iovad);
1851
Joerg Roedel86db2e52008-12-02 18:20:21 +01001852 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001853
Baoquan Hec3db9012016-09-15 16:50:52 +08001854 if (dom->domain.id)
1855 domain_id_free(dom->domain.id);
1856
Joerg Roedelec487d12008-06-26 21:27:58 +02001857 kfree(dom);
1858}
1859
Joerg Roedel431b2a22008-07-11 17:14:22 +02001860/*
1861 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001862 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001863 * structures required for the dma_ops interface
1864 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001865static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001866{
1867 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001868
1869 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1870 if (!dma_dom)
1871 return NULL;
1872
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001873 if (protection_domain_init(&dma_dom->domain))
Joerg Roedelec487d12008-06-26 21:27:58 +02001874 goto free_dma_dom;
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001875
Joerg Roedelffec2192016-07-26 15:31:23 +02001876 dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001877 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001878 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001879 if (!dma_dom->domain.pt_root)
1880 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001881
Zhen Leiaa3ac942017-09-21 16:52:45 +01001882 init_iova_domain(&dma_dom->iovad, PAGE_SIZE, IOVA_START_PFN);
Joerg Roedel307d5852016-07-05 11:54:04 +02001883
Joerg Roedel9003d612017-08-10 17:19:13 +02001884 if (init_iova_flush_queue(&dma_dom->iovad, iova_domain_flush_tlb, NULL))
Joerg Roedeld4241a22017-06-02 14:55:56 +02001885 goto free_dma_dom;
1886
Joerg Roedel9003d612017-08-10 17:19:13 +02001887 /* Initialize reserved ranges */
1888 copy_reserved_iova(&reserved_iova_ranges, &dma_dom->iovad);
Joerg Roedelfca6af62017-06-02 18:13:37 +02001889
Joerg Roedelec487d12008-06-26 21:27:58 +02001890 return dma_dom;
1891
1892free_dma_dom:
1893 dma_ops_domain_free(dma_dom);
1894
1895 return NULL;
1896}
1897
Joerg Roedel431b2a22008-07-11 17:14:22 +02001898/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001899 * little helper function to check whether a given protection domain is a
1900 * dma_ops domain
1901 */
1902static bool dma_ops_domain(struct protection_domain *domain)
1903{
1904 return domain->flags & PD_DMA_OPS_MASK;
1905}
1906
Gary R Hookff18c4e2017-12-20 09:47:08 -07001907static void set_dte_entry(u16 devid, struct protection_domain *domain,
1908 bool ats, bool ppr)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001909{
Joerg Roedel132bd682011-11-17 14:18:46 +01001910 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001911 u64 flags = 0;
Stuart Hayes36b72002019-09-05 12:09:48 -05001912 u32 old_domid;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001913
Joerg Roedel132bd682011-11-17 14:18:46 +01001914 if (domain->mode != PAGE_MODE_NONE)
Tom Lendacky2543a782017-07-17 16:10:24 -05001915 pte_root = iommu_virt_to_phys(domain->pt_root);
Joerg Roedel132bd682011-11-17 14:18:46 +01001916
Joerg Roedel38ddf412008-09-11 10:38:32 +02001917 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1918 << DEV_ENTRY_MODE_SHIFT;
Baoquan He07a80a62017-08-09 16:33:36 +08001919 pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V | DTE_FLAG_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001920
Joerg Roedelee6c2862011-11-09 12:06:03 +01001921 flags = amd_iommu_dev_table[devid].data[1];
1922
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001923 if (ats)
1924 flags |= DTE_FLAG_IOTLB;
1925
Gary R Hookff18c4e2017-12-20 09:47:08 -07001926 if (ppr) {
1927 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1928
1929 if (iommu_feature(iommu, FEATURE_EPHSUP))
1930 pte_root |= 1ULL << DEV_ENTRY_PPR;
1931 }
1932
Joerg Roedel52815b72011-11-17 17:24:28 +01001933 if (domain->flags & PD_IOMMUV2_MASK) {
Tom Lendacky2543a782017-07-17 16:10:24 -05001934 u64 gcr3 = iommu_virt_to_phys(domain->gcr3_tbl);
Joerg Roedel52815b72011-11-17 17:24:28 +01001935 u64 glx = domain->glx;
1936 u64 tmp;
1937
1938 pte_root |= DTE_FLAG_GV;
1939 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1940
1941 /* First mask out possible old values for GCR3 table */
1942 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1943 flags &= ~tmp;
1944
1945 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1946 flags &= ~tmp;
1947
1948 /* Encode GCR3 table into DTE */
1949 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1950 pte_root |= tmp;
1951
1952 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1953 flags |= tmp;
1954
1955 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1956 flags |= tmp;
1957 }
1958
Baoquan He45a01c42017-08-09 16:33:37 +08001959 flags &= ~DEV_DOMID_MASK;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001960 flags |= domain->id;
1961
Stuart Hayes36b72002019-09-05 12:09:48 -05001962 old_domid = amd_iommu_dev_table[devid].data[1] & DEV_DOMID_MASK;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001963 amd_iommu_dev_table[devid].data[1] = flags;
1964 amd_iommu_dev_table[devid].data[0] = pte_root;
Stuart Hayes36b72002019-09-05 12:09:48 -05001965
1966 /*
1967 * A kdump kernel might be replacing a domain ID that was copied from
1968 * the previous kernel--if so, it needs to flush the translation cache
1969 * entries for the old domain ID that is being overwritten
1970 */
1971 if (old_domid) {
1972 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1973
1974 amd_iommu_flush_tlb_domid(iommu, old_domid);
1975 }
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001976}
1977
Joerg Roedel15898bb2009-11-24 15:39:42 +01001978static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001979{
Joerg Roedel355bf552008-12-08 12:02:41 +01001980 /* remove entry from the device table seen by the hardware */
Baoquan He07a80a62017-08-09 16:33:36 +08001981 amd_iommu_dev_table[devid].data[0] = DTE_FLAG_V | DTE_FLAG_TV;
Joerg Roedelcbf3ccd2015-10-20 14:59:36 +02001982 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
Joerg Roedel355bf552008-12-08 12:02:41 +01001983
Joerg Roedelc5cca142009-10-09 18:31:20 +02001984 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001985}
1986
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001987static void do_attach(struct iommu_dev_data *dev_data,
1988 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001989{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001990 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001991 u16 alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001992 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001993
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001994 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001995 alias = dev_data->alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001996 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001997
1998 /* Update data structures */
1999 dev_data->domain = domain;
2000 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002001
2002 /* Do reference counting */
2003 domain->dev_iommu[iommu->index] += 1;
2004 domain->dev_cnt += 1;
2005
Joerg Roedele25bfb52015-10-20 17:33:38 +02002006 /* Update device table */
Gary R Hookff18c4e2017-12-20 09:47:08 -07002007 set_dte_entry(dev_data->devid, domain, ats, dev_data->iommu_v2);
Joerg Roedele25bfb52015-10-20 17:33:38 +02002008 if (alias != dev_data->devid)
Gary R Hookff18c4e2017-12-20 09:47:08 -07002009 set_dte_entry(alias, domain, ats, dev_data->iommu_v2);
Joerg Roedele25bfb52015-10-20 17:33:38 +02002010
Joerg Roedel6c542042011-06-09 17:07:31 +02002011 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002012}
2013
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002014static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002015{
Suravee Suthikulpanit9825bd92019-01-24 04:16:45 +00002016 struct protection_domain *domain = dev_data->domain;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002017 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02002018 u16 alias;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002019
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002020 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02002021 alias = dev_data->alias;
Joerg Roedelc5cca142009-10-09 18:31:20 +02002022
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002023 /* Update data structures */
2024 dev_data->domain = NULL;
2025 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002026 clear_dte_entry(dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02002027 if (alias != dev_data->devid)
2028 clear_dte_entry(alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002029
2030 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002031 device_flush_dte(dev_data);
Suravee Suthikulpanit9825bd92019-01-24 04:16:45 +00002032
2033 /* Flush IOTLB */
2034 domain_flush_tlb_pde(domain);
2035
2036 /* Wait for the flushes to finish */
2037 domain_flush_complete(domain);
2038
2039 /* decrease reference counters - needs to happen after the flushes */
2040 domain->dev_iommu[iommu->index] -= 1;
2041 domain->dev_cnt -= 1;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002042}
2043
2044/*
Anna-Maria Gleixner29a0c412018-05-07 14:53:26 +02002045 * If a device is not yet associated with a domain, this function makes the
2046 * device visible in the domain
Joerg Roedel15898bb2009-11-24 15:39:42 +01002047 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002048static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002049 struct protection_domain *domain)
2050{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002051 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002052
Joerg Roedel15898bb2009-11-24 15:39:42 +01002053 /* lock domain */
2054 spin_lock(&domain->lock);
2055
Joerg Roedel397111a2014-08-05 17:31:51 +02002056 ret = -EBUSY;
Joerg Roedel150952f2015-10-20 17:33:35 +02002057 if (dev_data->domain != NULL)
Joerg Roedel397111a2014-08-05 17:31:51 +02002058 goto out_unlock;
Joerg Roedel24100052009-11-25 15:59:57 +01002059
Joerg Roedel397111a2014-08-05 17:31:51 +02002060 /* Attach alias group root */
Joerg Roedel150952f2015-10-20 17:33:35 +02002061 do_attach(dev_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002062
Julia Lawall84fe6c12010-05-27 12:31:51 +02002063 ret = 0;
2064
2065out_unlock:
2066
Joerg Roedel355bf552008-12-08 12:02:41 +01002067 /* ready */
2068 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002069
Julia Lawall84fe6c12010-05-27 12:31:51 +02002070 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002071}
2072
Joerg Roedel52815b72011-11-17 17:24:28 +01002073
2074static void pdev_iommuv2_disable(struct pci_dev *pdev)
2075{
2076 pci_disable_ats(pdev);
2077 pci_disable_pri(pdev);
2078 pci_disable_pasid(pdev);
2079}
2080
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002081/* FIXME: Change generic reset-function to do the same */
2082static int pri_reset_while_enabled(struct pci_dev *pdev)
2083{
2084 u16 control;
2085 int pos;
2086
Joerg Roedel46277b72011-12-07 14:34:02 +01002087 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002088 if (!pos)
2089 return -EINVAL;
2090
Joerg Roedel46277b72011-12-07 14:34:02 +01002091 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2092 control |= PCI_PRI_CTRL_RESET;
2093 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002094
2095 return 0;
2096}
2097
Joerg Roedel52815b72011-11-17 17:24:28 +01002098static int pdev_iommuv2_enable(struct pci_dev *pdev)
2099{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002100 bool reset_enable;
2101 int reqs, ret;
2102
2103 /* FIXME: Hardcode number of outstanding requests for now */
2104 reqs = 32;
2105 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2106 reqs = 1;
2107 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002108
2109 /* Only allow access to user-accessible pages */
2110 ret = pci_enable_pasid(pdev, 0);
2111 if (ret)
2112 goto out_err;
2113
2114 /* First reset the PRI state of the device */
2115 ret = pci_reset_pri(pdev);
2116 if (ret)
2117 goto out_err;
2118
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002119 /* Enable PRI */
2120 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002121 if (ret)
2122 goto out_err;
2123
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002124 if (reset_enable) {
2125 ret = pri_reset_while_enabled(pdev);
2126 if (ret)
2127 goto out_err;
2128 }
2129
Joerg Roedel52815b72011-11-17 17:24:28 +01002130 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2131 if (ret)
2132 goto out_err;
2133
2134 return 0;
2135
2136out_err:
2137 pci_disable_pri(pdev);
2138 pci_disable_pasid(pdev);
2139
2140 return ret;
2141}
2142
Joerg Roedel15898bb2009-11-24 15:39:42 +01002143/*
Anna-Maria Gleixner29a0c412018-05-07 14:53:26 +02002144 * If a device is not yet associated with a domain, this function makes the
2145 * device visible in the domain
Joerg Roedel15898bb2009-11-24 15:39:42 +01002146 */
2147static int attach_device(struct device *dev,
2148 struct protection_domain *domain)
2149{
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002150 struct pci_dev *pdev;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002151 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002152 unsigned long flags;
2153 int ret;
2154
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002155 dev_data = get_dev_data(dev);
2156
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002157 if (!dev_is_pci(dev))
2158 goto skip_ats_check;
2159
2160 pdev = to_pci_dev(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002161 if (domain->flags & PD_IOMMUV2_MASK) {
Joerg Roedel02ca2022015-07-28 16:58:49 +02002162 if (!dev_data->passthrough)
Joerg Roedel52815b72011-11-17 17:24:28 +01002163 return -EINVAL;
2164
Joerg Roedel02ca2022015-07-28 16:58:49 +02002165 if (dev_data->iommu_v2) {
2166 if (pdev_iommuv2_enable(pdev) != 0)
2167 return -EINVAL;
Joerg Roedel52815b72011-11-17 17:24:28 +01002168
Joerg Roedel02ca2022015-07-28 16:58:49 +02002169 dev_data->ats.enabled = true;
2170 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Jean-Philippe Brucker83d18bd2019-04-10 16:21:08 +01002171 dev_data->pri_tlp = pci_prg_resp_pasid_required(pdev);
Joerg Roedel02ca2022015-07-28 16:58:49 +02002172 }
Joerg Roedel52815b72011-11-17 17:24:28 +01002173 } else if (amd_iommu_iotlb_sup &&
2174 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002175 dev_data->ats.enabled = true;
2176 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2177 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002178
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002179skip_ats_check:
Sebastian Andrzej Siewior2cd10832018-03-22 16:22:41 +01002180 spin_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002181 ret = __attach_device(dev_data, domain);
Sebastian Andrzej Siewior2cd10832018-03-22 16:22:41 +01002182 spin_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002183
2184 /*
2185 * We might boot into a crash-kernel here. The crashed kernel
2186 * left the caches in the IOMMU dirty. So we have to flush
2187 * here to evict all dirty stuff.
2188 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002189 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002190
2191 return ret;
2192}
2193
2194/*
2195 * Removes a device from a protection domain (unlocked)
2196 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002197static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002198{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002199 struct protection_domain *domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002200
Joerg Roedel2ca76272010-01-22 16:45:31 +01002201 domain = dev_data->domain;
2202
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002203 spin_lock(&domain->lock);
Joerg Roedel24100052009-11-25 15:59:57 +01002204
Joerg Roedel150952f2015-10-20 17:33:35 +02002205 do_detach(dev_data);
Joerg Roedel71f77582011-06-09 19:03:15 +02002206
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002207 spin_unlock(&domain->lock);
Joerg Roedel355bf552008-12-08 12:02:41 +01002208}
2209
2210/*
2211 * Removes a device from a protection domain (with devtable_lock held)
2212 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002213static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002214{
Joerg Roedel52815b72011-11-17 17:24:28 +01002215 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002216 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002217 unsigned long flags;
2218
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002219 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002220 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002221
Anna-Maria Gleixnerea3fd042018-05-07 14:53:27 +02002222 /*
2223 * First check if the device is still attached. It might already
2224 * be detached from its domain because the generic
2225 * iommu_detach_group code detached it and we try again here in
2226 * our alias handling.
2227 */
2228 if (WARN_ON(!dev_data->domain))
2229 return;
2230
Joerg Roedel355bf552008-12-08 12:02:41 +01002231 /* lock device table */
Sebastian Andrzej Siewior2cd10832018-03-22 16:22:41 +01002232 spin_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002233 __detach_device(dev_data);
Sebastian Andrzej Siewior2cd10832018-03-22 16:22:41 +01002234 spin_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002235
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002236 if (!dev_is_pci(dev))
2237 return;
2238
Joerg Roedel02ca2022015-07-28 16:58:49 +02002239 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
Joerg Roedel52815b72011-11-17 17:24:28 +01002240 pdev_iommuv2_disable(to_pci_dev(dev));
2241 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002242 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002243
2244 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002245}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002246
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002247static int amd_iommu_add_device(struct device *dev)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002248{
Joerg Roedel71f77582011-06-09 19:03:15 +02002249 struct iommu_dev_data *dev_data;
Joerg Roedel07ee8692015-05-28 18:41:42 +02002250 struct iommu_domain *domain;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002251 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002252 int ret, devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002253
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002254 if (!check_device(dev) || get_dev_data(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002255 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002256
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002257 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002258 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002259 return devid;
2260
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002261 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01002262
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002263 ret = iommu_init_device(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002264 if (ret) {
2265 if (ret != -ENOTSUPP)
Bjorn Helgaas5f226da2019-02-08 16:05:53 -06002266 dev_err(dev, "Failed to initialize - trying to proceed anyway\n");
Joerg Roedel657cbb62009-11-23 15:26:46 +01002267
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002268 iommu_ignore_device(dev);
Christoph Hellwig356da6d2018-12-06 13:39:32 -08002269 dev->dma_ops = NULL;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002270 goto out;
2271 }
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002272 init_iommu_group(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002273
Joerg Roedel07ee8692015-05-28 18:41:42 +02002274 dev_data = get_dev_data(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002275
2276 BUG_ON(!dev_data);
2277
Joerg Roedelcc7c8ad2019-08-19 15:22:49 +02002278 if (dev_data->iommu_v2)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002279 iommu_request_dm_for_dev(dev);
2280
2281 /* Domains are initialized for this device - have a look what we ended up with */
2282 domain = iommu_get_domain_for_dev(dev);
Joerg Roedel32302322015-07-28 16:58:50 +02002283 if (domain->type == IOMMU_DOMAIN_IDENTITY)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002284 dev_data->passthrough = true;
Joerg Roedel32302322015-07-28 16:58:50 +02002285 else
Bart Van Assche56579332017-01-20 13:04:02 -08002286 dev->dma_ops = &amd_iommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002287
2288out:
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002289 iommu_completion_wait(iommu);
2290
Joerg Roedele275a2a2008-12-10 18:27:25 +01002291 return 0;
2292}
2293
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002294static void amd_iommu_remove_device(struct device *dev)
Joerg Roedel8638c492009-12-10 11:12:25 +01002295{
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002296 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002297 int devid;
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002298
2299 if (!check_device(dev))
2300 return;
2301
2302 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002303 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002304 return;
2305
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002306 iommu = amd_iommu_rlookup_table[devid];
2307
2308 iommu_uninit_device(dev);
2309 iommu_completion_wait(iommu);
Joerg Roedel8638c492009-12-10 11:12:25 +01002310}
2311
Wan Zongshunb097d112016-04-01 09:06:04 -04002312static struct iommu_group *amd_iommu_device_group(struct device *dev)
2313{
2314 if (dev_is_pci(dev))
2315 return pci_device_group(dev);
2316
2317 return acpihid_device_group(dev);
2318}
2319
Joerg Roedel431b2a22008-07-11 17:14:22 +02002320/*****************************************************************************
2321 *
2322 * The next functions belong to the dma_ops mapping/unmapping code.
2323 *
2324 *****************************************************************************/
2325
2326/*
2327 * In the dma_ops path we only have the struct device. This function
2328 * finds the corresponding IOMMU, the protection domain and the
2329 * requestor id for a given device.
2330 * If the device is not yet associated with a domain this is also done
2331 * in this function.
2332 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002333static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002334{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002335 struct protection_domain *domain;
Baoquan Hedf3f7a62017-08-09 16:33:41 +08002336 struct iommu_domain *io_domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002337
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002338 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002339 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002340
Joerg Roedeld26592a2016-07-07 15:31:13 +02002341 domain = get_dev_data(dev)->domain;
Baoquan Hedf3f7a62017-08-09 16:33:41 +08002342 if (domain == NULL && get_dev_data(dev)->defer_attach) {
2343 get_dev_data(dev)->defer_attach = false;
2344 io_domain = iommu_get_domain_for_dev(dev);
2345 domain = to_pdomain(io_domain);
2346 attach_device(dev, domain);
2347 }
Baoquan Heec62b1a2017-08-24 21:13:57 +08002348 if (domain == NULL)
2349 return ERR_PTR(-EBUSY);
2350
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002351 if (!dma_ops_domain(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002352 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002353
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002354 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002355}
2356
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002357static void update_device_table(struct protection_domain *domain)
2358{
Joerg Roedel492667d2009-11-27 13:25:47 +01002359 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002360
Joerg Roedel3254de62016-07-26 15:18:54 +02002361 list_for_each_entry(dev_data, &domain->dev_list, list) {
Gary R Hookff18c4e2017-12-20 09:47:08 -07002362 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled,
2363 dev_data->iommu_v2);
Joerg Roedel3254de62016-07-26 15:18:54 +02002364
2365 if (dev_data->devid == dev_data->alias)
2366 continue;
2367
2368 /* There is an alias, update device table entry for it */
Gary R Hookff18c4e2017-12-20 09:47:08 -07002369 set_dte_entry(dev_data->alias, domain, dev_data->ats.enabled,
2370 dev_data->iommu_v2);
Joerg Roedel3254de62016-07-26 15:18:54 +02002371 }
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002372}
2373
2374static void update_domain(struct protection_domain *domain)
2375{
2376 if (!domain->updated)
2377 return;
2378
2379 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002380
2381 domain_flush_devices(domain);
2382 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002383
2384 domain->updated = false;
2385}
2386
Joerg Roedelf37f7f32016-07-08 11:47:22 +02002387static int dir2prot(enum dma_data_direction direction)
2388{
2389 if (direction == DMA_TO_DEVICE)
2390 return IOMMU_PROT_IR;
2391 else if (direction == DMA_FROM_DEVICE)
2392 return IOMMU_PROT_IW;
2393 else if (direction == DMA_BIDIRECTIONAL)
2394 return IOMMU_PROT_IW | IOMMU_PROT_IR;
2395 else
2396 return 0;
2397}
Baoquan Hedaae2d22017-08-09 16:33:43 +08002398
Joerg Roedel431b2a22008-07-11 17:14:22 +02002399/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002400 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002401 * contiguous memory region into DMA address space. It is used by all
2402 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002403 * Must be called with the domain lock held.
2404 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002405static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002406 struct dma_ops_domain *dma_dom,
2407 phys_addr_t paddr,
2408 size_t size,
Joerg Roedelf37f7f32016-07-08 11:47:22 +02002409 enum dma_data_direction direction,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002410 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002411{
2412 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002413 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002414 unsigned int pages;
Joerg Roedel518d9b42016-07-05 14:39:47 +02002415 int prot = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002416 int i;
2417
Joerg Roedele3c449f2008-10-15 22:02:11 -07002418 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002419 paddr &= PAGE_MASK;
2420
Joerg Roedel256e4622016-07-05 14:23:01 +02002421 address = dma_ops_alloc_iova(dev, dma_dom, pages, dma_mask);
Christoph Hellwigb3aa14f2018-11-21 19:28:34 +01002422 if (!address)
Joerg Roedel266a3bd2015-12-21 18:54:24 +01002423 goto out;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002424
Joerg Roedelf37f7f32016-07-08 11:47:22 +02002425 prot = dir2prot(direction);
Joerg Roedel518d9b42016-07-05 14:39:47 +02002426
Joerg Roedelcb76c322008-06-26 21:28:00 +02002427 start = address;
2428 for (i = 0; i < pages; ++i) {
Joerg Roedel518d9b42016-07-05 14:39:47 +02002429 ret = iommu_map_page(&dma_dom->domain, start, paddr,
2430 PAGE_SIZE, prot, GFP_ATOMIC);
2431 if (ret)
Joerg Roedel53812c12009-05-12 12:17:38 +02002432 goto out_unmap;
2433
Joerg Roedelcb76c322008-06-26 21:28:00 +02002434 paddr += PAGE_SIZE;
2435 start += PAGE_SIZE;
2436 }
2437 address += offset;
2438
Tom Murphy5cd3f2e2019-06-13 23:04:55 +01002439 domain_flush_np_cache(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002440
Joerg Roedelcb76c322008-06-26 21:28:00 +02002441out:
2442 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002443
2444out_unmap:
2445
2446 for (--i; i >= 0; --i) {
2447 start -= PAGE_SIZE;
Joerg Roedel518d9b42016-07-05 14:39:47 +02002448 iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
Joerg Roedel53812c12009-05-12 12:17:38 +02002449 }
2450
Joerg Roedel256e4622016-07-05 14:23:01 +02002451 domain_flush_tlb(&dma_dom->domain);
2452 domain_flush_complete(&dma_dom->domain);
2453
2454 dma_ops_free_iova(dma_dom, address, pages);
Joerg Roedel53812c12009-05-12 12:17:38 +02002455
Christoph Hellwigb3aa14f2018-11-21 19:28:34 +01002456 return DMA_MAPPING_ERROR;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002457}
2458
Joerg Roedel431b2a22008-07-11 17:14:22 +02002459/*
2460 * Does the reverse of the __map_single function. Must be called with
2461 * the domain lock held too
2462 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002463static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002464 dma_addr_t dma_addr,
2465 size_t size,
2466 int dir)
2467{
2468 dma_addr_t i, start;
2469 unsigned int pages;
2470
Joerg Roedele3c449f2008-10-15 22:02:11 -07002471 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002472 dma_addr &= PAGE_MASK;
2473 start = dma_addr;
2474
2475 for (i = 0; i < pages; ++i) {
Joerg Roedel518d9b42016-07-05 14:39:47 +02002476 iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002477 start += PAGE_SIZE;
2478 }
2479
Joerg Roedelb1516a12016-07-06 13:07:22 +02002480 if (amd_iommu_unmap_flush) {
Joerg Roedelb1516a12016-07-06 13:07:22 +02002481 domain_flush_tlb(&dma_dom->domain);
2482 domain_flush_complete(&dma_dom->domain);
Zhen Lei3c120142018-06-06 10:18:46 +08002483 dma_ops_free_iova(dma_dom, dma_addr, pages);
Joerg Roedelb1516a12016-07-06 13:07:22 +02002484 } else {
Joerg Roedel9003d612017-08-10 17:19:13 +02002485 pages = __roundup_pow_of_two(pages);
2486 queue_iova(&dma_dom->iovad, dma_addr >> PAGE_SHIFT, pages, 0);
Joerg Roedelb1516a12016-07-06 13:07:22 +02002487 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002488}
2489
Joerg Roedel431b2a22008-07-11 17:14:22 +02002490/*
2491 * The exported map_single function for dma_ops.
2492 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002493static dma_addr_t map_page(struct device *dev, struct page *page,
2494 unsigned long offset, size_t size,
2495 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002496 unsigned long attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002497{
FUJITA Tomonori51491362009-01-05 23:47:25 +09002498 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel89736a02019-05-06 14:24:18 +02002499 struct protection_domain *domain;
2500 struct dma_ops_domain *dma_dom;
2501 u64 dma_mask;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002502
Joerg Roedel89736a02019-05-06 14:24:18 +02002503 domain = get_domain(dev);
2504 if (PTR_ERR(domain) == -EINVAL)
2505 return (dma_addr_t)paddr;
2506 else if (IS_ERR(domain))
2507 return DMA_MAPPING_ERROR;
2508
2509 dma_mask = *dev->dma_mask;
2510 dma_dom = to_dma_ops_domain(domain);
2511
2512 return __map_single(dev, dma_dom, paddr, size, dir, dma_mask);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002513}
2514
Joerg Roedel431b2a22008-07-11 17:14:22 +02002515/*
2516 * The exported unmap_single function for dma_ops.
2517 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002518static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002519 enum dma_data_direction dir, unsigned long attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002520{
Joerg Roedel89736a02019-05-06 14:24:18 +02002521 struct protection_domain *domain;
2522 struct dma_ops_domain *dma_dom;
2523
2524 domain = get_domain(dev);
2525 if (IS_ERR(domain))
2526 return;
2527
2528 dma_dom = to_dma_ops_domain(domain);
Joerg Roedelb3311b02016-07-08 13:31:31 +02002529
2530 __unmap_single(dma_dom, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002531}
2532
Joerg Roedel80187fd2016-07-06 17:20:54 +02002533static int sg_num_pages(struct device *dev,
2534 struct scatterlist *sglist,
2535 int nelems)
2536{
2537 unsigned long mask, boundary_size;
2538 struct scatterlist *s;
2539 int i, npages = 0;
2540
2541 mask = dma_get_seg_boundary(dev);
2542 boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT :
2543 1UL << (BITS_PER_LONG - PAGE_SHIFT);
2544
2545 for_each_sg(sglist, s, nelems, i) {
2546 int p, n;
2547
2548 s->dma_address = npages << PAGE_SHIFT;
2549 p = npages % boundary_size;
2550 n = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
2551 if (p + n > boundary_size)
2552 npages += boundary_size - p;
2553 npages += n;
2554 }
2555
2556 return npages;
2557}
2558
Joerg Roedel431b2a22008-07-11 17:14:22 +02002559/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002560 * The exported map_sg function for dma_ops (handles scatter-gather
2561 * lists).
2562 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002563static int map_sg(struct device *dev, struct scatterlist *sglist,
Joerg Roedel80187fd2016-07-06 17:20:54 +02002564 int nelems, enum dma_data_direction direction,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002565 unsigned long attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002566{
Joerg Roedel80187fd2016-07-06 17:20:54 +02002567 int mapped_pages = 0, npages = 0, prot = 0, i;
Joerg Roedel89736a02019-05-06 14:24:18 +02002568 struct protection_domain *domain;
2569 struct dma_ops_domain *dma_dom;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002570 struct scatterlist *s;
Joerg Roedel80187fd2016-07-06 17:20:54 +02002571 unsigned long address;
Joerg Roedel89736a02019-05-06 14:24:18 +02002572 u64 dma_mask;
Jerry Snitselaar2e6c6a82019-01-28 17:59:37 -07002573 int ret;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002574
Joerg Roedel89736a02019-05-06 14:24:18 +02002575 domain = get_domain(dev);
2576 if (IS_ERR(domain))
2577 return 0;
2578
2579 dma_dom = to_dma_ops_domain(domain);
2580 dma_mask = *dev->dma_mask;
2581
Joerg Roedel80187fd2016-07-06 17:20:54 +02002582 npages = sg_num_pages(dev, sglist, nelems);
2583
2584 address = dma_ops_alloc_iova(dev, dma_dom, npages, dma_mask);
Qian Cai8cf66502019-07-11 12:17:45 -04002585 if (!address)
Joerg Roedel80187fd2016-07-06 17:20:54 +02002586 goto out_err;
2587
2588 prot = dir2prot(direction);
2589
2590 /* Map all sg entries */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002591 for_each_sg(sglist, s, nelems, i) {
Joerg Roedel80187fd2016-07-06 17:20:54 +02002592 int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002593
Joerg Roedel80187fd2016-07-06 17:20:54 +02002594 for (j = 0; j < pages; ++j) {
2595 unsigned long bus_addr, phys_addr;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002596
Joerg Roedel80187fd2016-07-06 17:20:54 +02002597 bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
2598 phys_addr = (sg_phys(s) & PAGE_MASK) + (j << PAGE_SHIFT);
Qian Cai3d708892019-08-28 17:39:43 -04002599 ret = iommu_map_page(domain, bus_addr, phys_addr,
2600 PAGE_SIZE, prot,
2601 GFP_ATOMIC | __GFP_NOWARN);
Joerg Roedel80187fd2016-07-06 17:20:54 +02002602 if (ret)
2603 goto out_unmap;
2604
2605 mapped_pages += 1;
2606 }
Joerg Roedel65b050a2008-06-26 21:28:02 +02002607 }
2608
Joerg Roedel80187fd2016-07-06 17:20:54 +02002609 /* Everything is mapped - write the right values into s->dma_address */
2610 for_each_sg(sglist, s, nelems, i) {
Stanislaw Gruszka4e50ce02019-03-13 10:03:17 +01002611 /*
2612 * Add in the remaining piece of the scatter-gather offset that
2613 * was masked out when we were determining the physical address
2614 * via (sg_phys(s) & PAGE_MASK) earlier.
2615 */
2616 s->dma_address += address + (s->offset & ~PAGE_MASK);
Joerg Roedel80187fd2016-07-06 17:20:54 +02002617 s->dma_length = s->length;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002618 }
2619
Tom Murphy5cd3f2e2019-06-13 23:04:55 +01002620 if (s)
2621 domain_flush_np_cache(domain, s->dma_address, s->dma_length);
2622
Joerg Roedel80187fd2016-07-06 17:20:54 +02002623 return nelems;
2624
2625out_unmap:
Bjorn Helgaas5f226da2019-02-08 16:05:53 -06002626 dev_err(dev, "IOMMU mapping error in map_sg (io-pages: %d reason: %d)\n",
2627 npages, ret);
Joerg Roedel80187fd2016-07-06 17:20:54 +02002628
2629 for_each_sg(sglist, s, nelems, i) {
2630 int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
2631
2632 for (j = 0; j < pages; ++j) {
2633 unsigned long bus_addr;
2634
2635 bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
2636 iommu_unmap_page(domain, bus_addr, PAGE_SIZE);
2637
Jerry Snitselaarf1724c02019-01-19 10:38:05 -07002638 if (--mapped_pages == 0)
Joerg Roedel80187fd2016-07-06 17:20:54 +02002639 goto out_free_iova;
2640 }
2641 }
2642
2643out_free_iova:
Jerry Snitselaar51d88382019-01-17 12:29:02 -07002644 free_iova_fast(&dma_dom->iovad, address >> PAGE_SHIFT, npages);
Joerg Roedel80187fd2016-07-06 17:20:54 +02002645
2646out_err:
Joerg Roedel92d420e2015-12-21 19:31:33 +01002647 return 0;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002648}
2649
Joerg Roedel431b2a22008-07-11 17:14:22 +02002650/*
2651 * The exported map_sg function for dma_ops (handles scatter-gather
2652 * lists).
2653 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002654static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002655 int nelems, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002656 unsigned long attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002657{
Joerg Roedel89736a02019-05-06 14:24:18 +02002658 struct protection_domain *domain;
2659 struct dma_ops_domain *dma_dom;
2660 unsigned long startaddr;
Colin Ian King2dbbcce2019-05-11 13:41:35 +01002661 int npages;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002662
Joerg Roedel89736a02019-05-06 14:24:18 +02002663 domain = get_domain(dev);
2664 if (IS_ERR(domain))
2665 return;
2666
2667 startaddr = sg_dma_address(sglist) & PAGE_MASK;
2668 dma_dom = to_dma_ops_domain(domain);
2669 npages = sg_num_pages(dev, sglist, nelems);
2670
2671 __unmap_single(dma_dom, startaddr, npages << PAGE_SHIFT, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002672}
2673
Joerg Roedel431b2a22008-07-11 17:14:22 +02002674/*
2675 * The exported alloc_coherent function for dma_ops.
2676 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002677static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002678 dma_addr_t *dma_addr, gfp_t flag,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002679 unsigned long attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002680{
Joerg Roedel832a90c2008-09-18 15:54:23 +02002681 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel89736a02019-05-06 14:24:18 +02002682 struct protection_domain *domain;
Linus Torvaldse16c4792018-06-11 12:22:12 -07002683 struct dma_ops_domain *dma_dom;
2684 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002685
Joerg Roedel89736a02019-05-06 14:24:18 +02002686 domain = get_domain(dev);
2687 if (PTR_ERR(domain) == -EINVAL) {
2688 page = alloc_pages(flag, get_order(size));
2689 *dma_addr = page_to_phys(page);
2690 return page_address(page);
2691 } else if (IS_ERR(domain))
Linus Torvaldse16c4792018-06-11 12:22:12 -07002692 return NULL;
2693
2694 dma_dom = to_dma_ops_domain(domain);
2695 size = PAGE_ALIGN(size);
2696 dma_mask = dev->coherent_dma_mask;
2697 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2698 flag |= __GFP_ZERO;
2699
2700 page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
2701 if (!page) {
2702 if (!gfpflags_allow_blocking(flag))
Joerg Roedel3b839a52015-04-01 14:58:47 +02002703 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002704
Linus Torvaldse16c4792018-06-11 12:22:12 -07002705 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
Marek Szyprowskid834c5a2018-08-17 15:49:00 -07002706 get_order(size), flag & __GFP_NOWARN);
Linus Torvaldse16c4792018-06-11 12:22:12 -07002707 if (!page)
2708 return NULL;
2709 }
Christoph Hellwigb4686202018-03-19 11:38:19 +01002710
Joerg Roedel832a90c2008-09-18 15:54:23 +02002711 if (!dma_mask)
2712 dma_mask = *dev->dma_mask;
2713
Linus Torvaldse16c4792018-06-11 12:22:12 -07002714 *dma_addr = __map_single(dev, dma_dom, page_to_phys(page),
2715 size, DMA_BIDIRECTIONAL, dma_mask);
2716
Christoph Hellwigb3aa14f2018-11-21 19:28:34 +01002717 if (*dma_addr == DMA_MAPPING_ERROR)
Joerg Roedel5b28df62008-12-02 17:49:42 +01002718 goto out_free;
Linus Torvaldse16c4792018-06-11 12:22:12 -07002719
2720 return page_address(page);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002721
2722out_free:
Linus Torvaldse16c4792018-06-11 12:22:12 -07002723
2724 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2725 __free_pages(page, get_order(size));
2726
Joerg Roedel5b28df62008-12-02 17:49:42 +01002727 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002728}
2729
Joerg Roedel431b2a22008-07-11 17:14:22 +02002730/*
2731 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002732 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002733static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002734 void *virt_addr, dma_addr_t dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002735 unsigned long attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002736{
Joerg Roedel89736a02019-05-06 14:24:18 +02002737 struct protection_domain *domain;
2738 struct dma_ops_domain *dma_dom;
2739 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002740
Joerg Roedel89736a02019-05-06 14:24:18 +02002741 page = virt_to_page(virt_addr);
Joerg Roedel3b839a52015-04-01 14:58:47 +02002742 size = PAGE_ALIGN(size);
2743
Joerg Roedel89736a02019-05-06 14:24:18 +02002744 domain = get_domain(dev);
2745 if (IS_ERR(domain))
2746 goto free_mem;
2747
2748 dma_dom = to_dma_ops_domain(domain);
2749
Linus Torvaldse16c4792018-06-11 12:22:12 -07002750 __unmap_single(dma_dom, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel89736a02019-05-06 14:24:18 +02002751
2752free_mem:
Linus Torvaldse16c4792018-06-11 12:22:12 -07002753 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2754 __free_pages(page, get_order(size));
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002755}
2756
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002757/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002758 * This function is called by the DMA layer to find out if we can handle a
2759 * particular device. It is part of the dma_ops.
2760 */
2761static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2762{
Christoph Hellwigfec777c2018-03-19 11:38:15 +01002763 if (!dma_direct_supported(dev, mask))
Christoph Hellwig5860acc2017-05-22 11:38:27 +02002764 return 0;
Joerg Roedel420aef82009-11-23 16:14:57 +01002765 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002766}
2767
Bart Van Assche52997092017-01-20 13:04:01 -08002768static const struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedela639a8e2015-12-22 16:06:49 +01002769 .alloc = alloc_coherent,
2770 .free = free_coherent,
2771 .map_page = map_page,
2772 .unmap_page = unmap_page,
2773 .map_sg = map_sg,
2774 .unmap_sg = unmap_sg,
2775 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002776};
2777
Joerg Roedel81cd07b2016-07-07 18:01:10 +02002778static int init_reserved_iova_ranges(void)
2779{
2780 struct pci_dev *pdev = NULL;
2781 struct iova *val;
2782
Zhen Leiaa3ac942017-09-21 16:52:45 +01002783 init_iova_domain(&reserved_iova_ranges, PAGE_SIZE, IOVA_START_PFN);
Joerg Roedel81cd07b2016-07-07 18:01:10 +02002784
2785 lockdep_set_class(&reserved_iova_ranges.iova_rbtree_lock,
2786 &reserved_rbtree_key);
2787
2788 /* MSI memory range */
2789 val = reserve_iova(&reserved_iova_ranges,
2790 IOVA_PFN(MSI_RANGE_START), IOVA_PFN(MSI_RANGE_END));
2791 if (!val) {
2792 pr_err("Reserving MSI range failed\n");
2793 return -ENOMEM;
2794 }
2795
2796 /* HT memory range */
2797 val = reserve_iova(&reserved_iova_ranges,
2798 IOVA_PFN(HT_RANGE_START), IOVA_PFN(HT_RANGE_END));
2799 if (!val) {
2800 pr_err("Reserving HT range failed\n");
2801 return -ENOMEM;
2802 }
2803
2804 /*
2805 * Memory used for PCI resources
2806 * FIXME: Check whether we can reserve the PCI-hole completly
2807 */
2808 for_each_pci_dev(pdev) {
2809 int i;
2810
2811 for (i = 0; i < PCI_NUM_RESOURCES; ++i) {
2812 struct resource *r = &pdev->resource[i];
2813
2814 if (!(r->flags & IORESOURCE_MEM))
2815 continue;
2816
2817 val = reserve_iova(&reserved_iova_ranges,
2818 IOVA_PFN(r->start),
2819 IOVA_PFN(r->end));
2820 if (!val) {
Bjorn Helgaas5f226da2019-02-08 16:05:53 -06002821 pci_err(pdev, "Reserve pci-resource range %pR failed\n", r);
Joerg Roedel81cd07b2016-07-07 18:01:10 +02002822 return -ENOMEM;
2823 }
2824 }
2825 }
2826
2827 return 0;
2828}
2829
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002830int __init amd_iommu_init_api(void)
Joerg Roedel27c21272011-05-30 15:56:24 +02002831{
Joerg Roedel460c26d2017-06-02 14:28:01 +02002832 int ret, err = 0;
Joerg Roedel307d5852016-07-05 11:54:04 +02002833
2834 ret = iova_cache_get();
2835 if (ret)
2836 return ret;
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002837
Joerg Roedel81cd07b2016-07-07 18:01:10 +02002838 ret = init_reserved_iova_ranges();
2839 if (ret)
2840 return ret;
2841
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002842 err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
2843 if (err)
2844 return err;
2845#ifdef CONFIG_ARM_AMBA
2846 err = bus_set_iommu(&amba_bustype, &amd_iommu_ops);
2847 if (err)
2848 return err;
2849#endif
Wan Zongshun0076cd32016-05-10 09:21:01 -04002850 err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops);
2851 if (err)
2852 return err;
Joerg Roedel460c26d2017-06-02 14:28:01 +02002853
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002854 return 0;
Joerg Roedelf5325092010-01-22 17:44:35 +01002855}
2856
Joerg Roedel6631ee92008-06-26 21:28:05 +02002857int __init amd_iommu_init_dma_ops(void)
2858{
Joerg Roedelcc7c8ad2019-08-19 15:22:49 +02002859 swiotlb = (iommu_default_passthrough() || sme_me_mask) ? 1 : 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002860 iommu_detected = 1;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002861
Joerg Roedel62410ee2012-06-12 16:42:43 +02002862 if (amd_iommu_unmap_flush)
Joerg Roedel101fa032018-11-27 16:22:31 +01002863 pr_info("IO/TLB flush on unmap enabled\n");
Joerg Roedel62410ee2012-06-12 16:42:43 +02002864 else
Joerg Roedel101fa032018-11-27 16:22:31 +01002865 pr_info("Lazy IO/TLB flushing enabled\n");
Joerg Roedel62410ee2012-06-12 16:42:43 +02002866
Joerg Roedel6631ee92008-06-26 21:28:05 +02002867 return 0;
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002868
Joerg Roedel6631ee92008-06-26 21:28:05 +02002869}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002870
2871/*****************************************************************************
2872 *
2873 * The following functions belong to the exported interface of AMD IOMMU
2874 *
2875 * This interface allows access to lower level functions of the IOMMU
2876 * like protection domain handling and assignement of devices to domains
2877 * which is not possible with the dma_ops interface.
2878 *
2879 *****************************************************************************/
2880
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002881static void cleanup_domain(struct protection_domain *domain)
2882{
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002883 struct iommu_dev_data *entry;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002884 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002885
Sebastian Andrzej Siewior2cd10832018-03-22 16:22:41 +01002886 spin_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002887
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002888 while (!list_empty(&domain->dev_list)) {
2889 entry = list_first_entry(&domain->dev_list,
2890 struct iommu_dev_data, list);
Anna-Maria Gleixnerea3fd042018-05-07 14:53:27 +02002891 BUG_ON(!entry->domain);
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002892 __detach_device(entry);
Joerg Roedel492667d2009-11-27 13:25:47 +01002893 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002894
Sebastian Andrzej Siewior2cd10832018-03-22 16:22:41 +01002895 spin_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002896}
2897
Joerg Roedel26508152009-08-26 16:52:40 +02002898static void protection_domain_free(struct protection_domain *domain)
2899{
2900 if (!domain)
2901 return;
2902
2903 if (domain->id)
2904 domain_id_free(domain->id);
2905
2906 kfree(domain);
2907}
2908
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002909static int protection_domain_init(struct protection_domain *domain)
2910{
2911 spin_lock_init(&domain->lock);
2912 mutex_init(&domain->api_lock);
2913 domain->id = domain_id_alloc();
2914 if (!domain->id)
2915 return -ENOMEM;
2916 INIT_LIST_HEAD(&domain->dev_list);
2917
2918 return 0;
2919}
2920
Joerg Roedel26508152009-08-26 16:52:40 +02002921static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002922{
2923 struct protection_domain *domain;
2924
2925 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2926 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002927 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002928
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002929 if (protection_domain_init(domain))
Joerg Roedel26508152009-08-26 16:52:40 +02002930 goto out_err;
2931
2932 return domain;
2933
2934out_err:
2935 kfree(domain);
2936
2937 return NULL;
2938}
2939
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002940static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2941{
2942 struct protection_domain *pdomain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002943 struct dma_ops_domain *dma_domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002944
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002945 switch (type) {
2946 case IOMMU_DOMAIN_UNMANAGED:
2947 pdomain = protection_domain_alloc();
2948 if (!pdomain)
2949 return NULL;
2950
2951 pdomain->mode = PAGE_MODE_3_LEVEL;
2952 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2953 if (!pdomain->pt_root) {
2954 protection_domain_free(pdomain);
2955 return NULL;
2956 }
2957
2958 pdomain->domain.geometry.aperture_start = 0;
2959 pdomain->domain.geometry.aperture_end = ~0ULL;
2960 pdomain->domain.geometry.force_aperture = true;
2961
2962 break;
2963 case IOMMU_DOMAIN_DMA:
2964 dma_domain = dma_ops_domain_alloc();
2965 if (!dma_domain) {
Joerg Roedel101fa032018-11-27 16:22:31 +01002966 pr_err("Failed to allocate\n");
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002967 return NULL;
2968 }
2969 pdomain = &dma_domain->domain;
2970 break;
Joerg Roedel07f643a2015-05-28 18:41:41 +02002971 case IOMMU_DOMAIN_IDENTITY:
2972 pdomain = protection_domain_alloc();
2973 if (!pdomain)
2974 return NULL;
2975
2976 pdomain->mode = PAGE_MODE_NONE;
2977 break;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002978 default:
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002979 return NULL;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002980 }
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002981
2982 return &pdomain->domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002983}
2984
2985static void amd_iommu_domain_free(struct iommu_domain *dom)
Joerg Roedel26508152009-08-26 16:52:40 +02002986{
2987 struct protection_domain *domain;
Joerg Roedelcda70052016-07-07 15:57:04 +02002988 struct dma_ops_domain *dma_dom;
Joerg Roedel98383fc2008-12-02 18:34:12 +01002989
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002990 domain = to_pdomain(dom);
2991
Joerg Roedel98383fc2008-12-02 18:34:12 +01002992 if (domain->dev_cnt > 0)
2993 cleanup_domain(domain);
2994
2995 BUG_ON(domain->dev_cnt != 0);
2996
Joerg Roedelcda70052016-07-07 15:57:04 +02002997 if (!dom)
2998 return;
Joerg Roedel98383fc2008-12-02 18:34:12 +01002999
Joerg Roedelcda70052016-07-07 15:57:04 +02003000 switch (dom->type) {
3001 case IOMMU_DOMAIN_DMA:
Joerg Roedel281e8cc2016-07-07 16:12:02 +02003002 /* Now release the domain */
Joerg Roedelb3311b02016-07-08 13:31:31 +02003003 dma_dom = to_dma_ops_domain(domain);
Joerg Roedelcda70052016-07-07 15:57:04 +02003004 dma_ops_domain_free(dma_dom);
3005 break;
3006 default:
3007 if (domain->mode != PAGE_MODE_NONE)
3008 free_pagetable(domain);
Joerg Roedel52815b72011-11-17 17:24:28 +01003009
Joerg Roedelcda70052016-07-07 15:57:04 +02003010 if (domain->flags & PD_IOMMUV2_MASK)
3011 free_gcr3_table(domain);
3012
3013 protection_domain_free(domain);
3014 break;
3015 }
Joerg Roedel98383fc2008-12-02 18:34:12 +01003016}
3017
Joerg Roedel684f2882008-12-08 12:07:44 +01003018static void amd_iommu_detach_device(struct iommu_domain *dom,
3019 struct device *dev)
3020{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003021 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003022 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003023 int devid;
Joerg Roedel684f2882008-12-08 12:07:44 +01003024
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003025 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003026 return;
3027
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003028 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02003029 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003030 return;
Joerg Roedel684f2882008-12-08 12:07:44 +01003031
Joerg Roedel657cbb62009-11-23 15:26:46 +01003032 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003033 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003034
3035 iommu = amd_iommu_rlookup_table[devid];
3036 if (!iommu)
3037 return;
3038
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05003039#ifdef CONFIG_IRQ_REMAP
3040 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
3041 (dom->type == IOMMU_DOMAIN_UNMANAGED))
3042 dev_data->use_vapic = 0;
3043#endif
3044
Joerg Roedel684f2882008-12-08 12:07:44 +01003045 iommu_completion_wait(iommu);
3046}
3047
Joerg Roedel01106062008-12-02 19:34:11 +01003048static int amd_iommu_attach_device(struct iommu_domain *dom,
3049 struct device *dev)
3050{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003051 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel657cbb62009-11-23 15:26:46 +01003052 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003053 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003054 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003055
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003056 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003057 return -EINVAL;
3058
Joerg Roedel657cbb62009-11-23 15:26:46 +01003059 dev_data = dev->archdata.iommu;
3060
Joerg Roedelf62dda62011-06-09 12:55:35 +02003061 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003062 if (!iommu)
3063 return -EINVAL;
3064
Joerg Roedel657cbb62009-11-23 15:26:46 +01003065 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003066 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003067
Joerg Roedel15898bb2009-11-24 15:39:42 +01003068 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003069
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05003070#ifdef CONFIG_IRQ_REMAP
3071 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
3072 if (dom->type == IOMMU_DOMAIN_UNMANAGED)
3073 dev_data->use_vapic = 1;
3074 else
3075 dev_data->use_vapic = 0;
3076 }
3077#endif
3078
Joerg Roedel01106062008-12-02 19:34:11 +01003079 iommu_completion_wait(iommu);
3080
Joerg Roedel15898bb2009-11-24 15:39:42 +01003081 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003082}
3083
Joerg Roedel468e2362010-01-21 16:37:36 +01003084static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003085 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003086{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003087 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003088 int prot = 0;
3089 int ret;
3090
Joerg Roedel132bd682011-11-17 14:18:46 +01003091 if (domain->mode == PAGE_MODE_NONE)
3092 return -EINVAL;
3093
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003094 if (iommu_prot & IOMMU_READ)
3095 prot |= IOMMU_PROT_IR;
3096 if (iommu_prot & IOMMU_WRITE)
3097 prot |= IOMMU_PROT_IW;
3098
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003099 mutex_lock(&domain->api_lock);
Joerg Roedelb911b892016-07-05 14:29:11 +02003100 ret = iommu_map_page(domain, iova, paddr, page_size, prot, GFP_KERNEL);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003101 mutex_unlock(&domain->api_lock);
3102
Tom Murphy5cd3f2e2019-06-13 23:04:55 +01003103 domain_flush_np_cache(domain, iova, page_size);
3104
Joerg Roedel795e74f72010-05-11 17:40:57 +02003105 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003106}
3107
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003108static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
Will Deacon56f8af52019-07-02 16:44:06 +01003109 size_t page_size,
3110 struct iommu_iotlb_gather *gather)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003111{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003112 struct protection_domain *domain = to_pdomain(dom);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003113 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003114
Joerg Roedel132bd682011-11-17 14:18:46 +01003115 if (domain->mode == PAGE_MODE_NONE)
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05003116 return 0;
Joerg Roedel132bd682011-11-17 14:18:46 +01003117
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003118 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003119 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003120 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003121
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003122 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003123}
3124
Joerg Roedel645c4c82008-12-02 20:05:50 +01003125static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547a2013-03-29 01:23:58 +05303126 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01003127{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003128 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel3039ca12015-04-01 14:58:48 +02003129 unsigned long offset_mask, pte_pgsize;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003130 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003131
Joerg Roedel132bd682011-11-17 14:18:46 +01003132 if (domain->mode == PAGE_MODE_NONE)
3133 return iova;
3134
Joerg Roedel3039ca12015-04-01 14:58:48 +02003135 pte = fetch_pte(domain, iova, &pte_pgsize);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003136
Joerg Roedela6d41a42009-09-02 17:08:55 +02003137 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003138 return 0;
3139
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003140 offset_mask = pte_pgsize - 1;
Singh, Brijeshb3e9b512018-10-04 21:40:23 +00003141 __pte = __sme_clr(*pte & PM_ADDR_MASK);
Joerg Roedelf03152b2010-01-21 16:15:24 +01003142
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003143 return (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003144}
3145
Joerg Roedelab636482014-09-05 10:48:21 +02003146static bool amd_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003147{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003148 switch (cap) {
3149 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedelab636482014-09-05 10:48:21 +02003150 return true;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003151 case IOMMU_CAP_INTR_REMAP:
Joerg Roedelab636482014-09-05 10:48:21 +02003152 return (irq_remapping_enabled == 1);
Will Deaconcfdeec22014-10-27 11:24:48 +00003153 case IOMMU_CAP_NOEXEC:
3154 return false;
Lu Baolue84b7cc2018-10-08 10:24:19 +08003155 default:
3156 break;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003157 }
3158
Joerg Roedelab636482014-09-05 10:48:21 +02003159 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003160}
3161
Eric Augere5b52342017-01-19 20:57:47 +00003162static void amd_iommu_get_resv_regions(struct device *dev,
3163 struct list_head *head)
Joerg Roedel35cf2482015-05-28 18:41:37 +02003164{
Eric Auger4397f322017-01-19 20:57:54 +00003165 struct iommu_resv_region *region;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003166 struct unity_map_entry *entry;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003167 int devid;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003168
3169 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02003170 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003171 return;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003172
3173 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
Joerg Roedel8aafaaf2019-03-28 11:44:59 +01003174 int type, prot = 0;
Eric Auger4397f322017-01-19 20:57:54 +00003175 size_t length;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003176
3177 if (devid < entry->devid_start || devid > entry->devid_end)
3178 continue;
3179
Joerg Roedel8aafaaf2019-03-28 11:44:59 +01003180 type = IOMMU_RESV_DIRECT;
Eric Auger4397f322017-01-19 20:57:54 +00003181 length = entry->address_end - entry->address_start;
3182 if (entry->prot & IOMMU_PROT_IR)
3183 prot |= IOMMU_READ;
3184 if (entry->prot & IOMMU_PROT_IW)
3185 prot |= IOMMU_WRITE;
Joerg Roedel8aafaaf2019-03-28 11:44:59 +01003186 if (entry->prot & IOMMU_UNITY_MAP_FLAG_EXCL_RANGE)
3187 /* Exclusion range */
3188 type = IOMMU_RESV_RESERVED;
Eric Auger4397f322017-01-19 20:57:54 +00003189
3190 region = iommu_alloc_resv_region(entry->address_start,
Joerg Roedel8aafaaf2019-03-28 11:44:59 +01003191 length, prot, type);
Joerg Roedel35cf2482015-05-28 18:41:37 +02003192 if (!region) {
Bjorn Helgaas5f226da2019-02-08 16:05:53 -06003193 dev_err(dev, "Out of memory allocating dm-regions\n");
Joerg Roedel35cf2482015-05-28 18:41:37 +02003194 return;
3195 }
Joerg Roedel35cf2482015-05-28 18:41:37 +02003196 list_add_tail(&region->list, head);
3197 }
Eric Auger4397f322017-01-19 20:57:54 +00003198
3199 region = iommu_alloc_resv_region(MSI_RANGE_START,
3200 MSI_RANGE_END - MSI_RANGE_START + 1,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00003201 0, IOMMU_RESV_MSI);
Eric Auger4397f322017-01-19 20:57:54 +00003202 if (!region)
3203 return;
3204 list_add_tail(&region->list, head);
3205
3206 region = iommu_alloc_resv_region(HT_RANGE_START,
3207 HT_RANGE_END - HT_RANGE_START + 1,
3208 0, IOMMU_RESV_RESERVED);
3209 if (!region)
3210 return;
3211 list_add_tail(&region->list, head);
Joerg Roedel35cf2482015-05-28 18:41:37 +02003212}
3213
Eric Augere5b52342017-01-19 20:57:47 +00003214static void amd_iommu_put_resv_regions(struct device *dev,
Joerg Roedel35cf2482015-05-28 18:41:37 +02003215 struct list_head *head)
3216{
Eric Augere5b52342017-01-19 20:57:47 +00003217 struct iommu_resv_region *entry, *next;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003218
3219 list_for_each_entry_safe(entry, next, head, list)
3220 kfree(entry);
3221}
3222
Eric Augere5b52342017-01-19 20:57:47 +00003223static void amd_iommu_apply_resv_region(struct device *dev,
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02003224 struct iommu_domain *domain,
Eric Augere5b52342017-01-19 20:57:47 +00003225 struct iommu_resv_region *region)
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02003226{
Joerg Roedelb3311b02016-07-08 13:31:31 +02003227 struct dma_ops_domain *dma_dom = to_dma_ops_domain(to_pdomain(domain));
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02003228 unsigned long start, end;
3229
3230 start = IOVA_PFN(region->start);
Gary R Hookb92b4fb2017-11-03 10:50:34 -06003231 end = IOVA_PFN(region->start + region->length - 1);
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02003232
3233 WARN_ON_ONCE(reserve_iova(&dma_dom->iovad, start, end) == NULL);
3234}
3235
Baoquan Hedf3f7a62017-08-09 16:33:41 +08003236static bool amd_iommu_is_attach_deferred(struct iommu_domain *domain,
3237 struct device *dev)
3238{
3239 struct iommu_dev_data *dev_data = dev->archdata.iommu;
3240 return dev_data->defer_attach;
3241}
3242
Suravee Suthikulpaniteb5ecd12018-02-21 14:19:45 +07003243static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain)
3244{
3245 struct protection_domain *dom = to_pdomain(domain);
3246
3247 domain_flush_tlb_pde(dom);
3248 domain_flush_complete(dom);
3249}
3250
Will Deacon56f8af52019-07-02 16:44:06 +01003251static void amd_iommu_iotlb_sync(struct iommu_domain *domain,
3252 struct iommu_iotlb_gather *gather)
Suravee Suthikulpaniteb5ecd12018-02-21 14:19:45 +07003253{
Will Deacon56f8af52019-07-02 16:44:06 +01003254 amd_iommu_flush_iotlb_all(domain);
Suravee Suthikulpaniteb5ecd12018-02-21 14:19:45 +07003255}
3256
Joerg Roedelb0119e82017-02-01 13:23:08 +01003257const struct iommu_ops amd_iommu_ops = {
Joerg Roedelab636482014-09-05 10:48:21 +02003258 .capable = amd_iommu_capable,
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003259 .domain_alloc = amd_iommu_domain_alloc,
3260 .domain_free = amd_iommu_domain_free,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003261 .attach_dev = amd_iommu_attach_device,
3262 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003263 .map = amd_iommu_map,
3264 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003265 .iova_to_phys = amd_iommu_iova_to_phys,
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02003266 .add_device = amd_iommu_add_device,
3267 .remove_device = amd_iommu_remove_device,
Wan Zongshunb097d112016-04-01 09:06:04 -04003268 .device_group = amd_iommu_device_group,
Eric Augere5b52342017-01-19 20:57:47 +00003269 .get_resv_regions = amd_iommu_get_resv_regions,
3270 .put_resv_regions = amd_iommu_put_resv_regions,
3271 .apply_resv_region = amd_iommu_apply_resv_region,
Baoquan Hedf3f7a62017-08-09 16:33:41 +08003272 .is_attach_deferred = amd_iommu_is_attach_deferred,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003273 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Suravee Suthikulpaniteb5ecd12018-02-21 14:19:45 +07003274 .flush_iotlb_all = amd_iommu_flush_iotlb_all,
Will Deacon56f8af52019-07-02 16:44:06 +01003275 .iotlb_sync = amd_iommu_iotlb_sync,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003276};
3277
Joerg Roedel0feae532009-08-26 15:26:30 +02003278/*****************************************************************************
3279 *
3280 * The next functions do a basic initialization of IOMMU for pass through
3281 * mode
3282 *
3283 * In passthrough mode the IOMMU is initialized and enabled but not used for
3284 * DMA-API translation.
3285 *
3286 *****************************************************************************/
3287
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003288/* IOMMUv2 specific functions */
3289int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3290{
3291 return atomic_notifier_chain_register(&ppr_notifier, nb);
3292}
3293EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3294
3295int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3296{
3297 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3298}
3299EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003300
3301void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3302{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003303 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel132bd682011-11-17 14:18:46 +01003304 unsigned long flags;
3305
3306 spin_lock_irqsave(&domain->lock, flags);
3307
3308 /* Update data structure */
3309 domain->mode = PAGE_MODE_NONE;
3310 domain->updated = true;
3311
3312 /* Make changes visible to IOMMUs */
3313 update_domain(domain);
3314
3315 /* Page-table is not visible to IOMMU anymore, so free it */
3316 free_pagetable(domain);
3317
3318 spin_unlock_irqrestore(&domain->lock, flags);
3319}
3320EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003321
3322int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3323{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003324 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel52815b72011-11-17 17:24:28 +01003325 unsigned long flags;
3326 int levels, ret;
3327
3328 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3329 return -EINVAL;
3330
3331 /* Number of GCR3 table levels required */
3332 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3333 levels += 1;
3334
3335 if (levels > amd_iommu_max_glx_val)
3336 return -EINVAL;
3337
3338 spin_lock_irqsave(&domain->lock, flags);
3339
3340 /*
3341 * Save us all sanity checks whether devices already in the
3342 * domain support IOMMUv2. Just force that the domain has no
3343 * devices attached when it is switched into IOMMUv2 mode.
3344 */
3345 ret = -EBUSY;
3346 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3347 goto out;
3348
3349 ret = -ENOMEM;
3350 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3351 if (domain->gcr3_tbl == NULL)
3352 goto out;
3353
3354 domain->glx = levels;
3355 domain->flags |= PD_IOMMUV2_MASK;
3356 domain->updated = true;
3357
3358 update_domain(domain);
3359
3360 ret = 0;
3361
3362out:
3363 spin_unlock_irqrestore(&domain->lock, flags);
3364
3365 return ret;
3366}
3367EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003368
3369static int __flush_pasid(struct protection_domain *domain, int pasid,
3370 u64 address, bool size)
3371{
3372 struct iommu_dev_data *dev_data;
3373 struct iommu_cmd cmd;
3374 int i, ret;
3375
3376 if (!(domain->flags & PD_IOMMUV2_MASK))
3377 return -EINVAL;
3378
3379 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3380
3381 /*
3382 * IOMMU TLB needs to be flushed before Device TLB to
3383 * prevent device TLB refill from IOMMU TLB
3384 */
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -06003385 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
Joerg Roedel22e266c2011-11-21 15:59:08 +01003386 if (domain->dev_iommu[i] == 0)
3387 continue;
3388
3389 ret = iommu_queue_command(amd_iommus[i], &cmd);
3390 if (ret != 0)
3391 goto out;
3392 }
3393
3394 /* Wait until IOMMU TLB flushes are complete */
3395 domain_flush_complete(domain);
3396
3397 /* Now flush device TLBs */
3398 list_for_each_entry(dev_data, &domain->dev_list, list) {
3399 struct amd_iommu *iommu;
3400 int qdep;
3401
Joerg Roedel1c1cc452015-07-30 11:24:45 +02003402 /*
3403 There might be non-IOMMUv2 capable devices in an IOMMUv2
3404 * domain.
3405 */
3406 if (!dev_data->ats.enabled)
3407 continue;
Joerg Roedel22e266c2011-11-21 15:59:08 +01003408
3409 qdep = dev_data->ats.qdep;
3410 iommu = amd_iommu_rlookup_table[dev_data->devid];
3411
3412 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3413 qdep, address, size);
3414
3415 ret = iommu_queue_command(iommu, &cmd);
3416 if (ret != 0)
3417 goto out;
3418 }
3419
3420 /* Wait until all device TLBs are flushed */
3421 domain_flush_complete(domain);
3422
3423 ret = 0;
3424
3425out:
3426
3427 return ret;
3428}
3429
3430static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3431 u64 address)
3432{
3433 return __flush_pasid(domain, pasid, address, false);
3434}
3435
3436int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3437 u64 address)
3438{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003439 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003440 unsigned long flags;
3441 int ret;
3442
3443 spin_lock_irqsave(&domain->lock, flags);
3444 ret = __amd_iommu_flush_page(domain, pasid, address);
3445 spin_unlock_irqrestore(&domain->lock, flags);
3446
3447 return ret;
3448}
3449EXPORT_SYMBOL(amd_iommu_flush_page);
3450
3451static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3452{
3453 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3454 true);
3455}
3456
3457int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3458{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003459 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003460 unsigned long flags;
3461 int ret;
3462
3463 spin_lock_irqsave(&domain->lock, flags);
3464 ret = __amd_iommu_flush_tlb(domain, pasid);
3465 spin_unlock_irqrestore(&domain->lock, flags);
3466
3467 return ret;
3468}
3469EXPORT_SYMBOL(amd_iommu_flush_tlb);
3470
Joerg Roedelb16137b2011-11-21 16:50:23 +01003471static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3472{
3473 int index;
3474 u64 *pte;
3475
3476 while (true) {
3477
3478 index = (pasid >> (9 * level)) & 0x1ff;
3479 pte = &root[index];
3480
3481 if (level == 0)
3482 break;
3483
3484 if (!(*pte & GCR3_VALID)) {
3485 if (!alloc)
3486 return NULL;
3487
3488 root = (void *)get_zeroed_page(GFP_ATOMIC);
3489 if (root == NULL)
3490 return NULL;
3491
Tom Lendacky2543a782017-07-17 16:10:24 -05003492 *pte = iommu_virt_to_phys(root) | GCR3_VALID;
Joerg Roedelb16137b2011-11-21 16:50:23 +01003493 }
3494
Tom Lendacky2543a782017-07-17 16:10:24 -05003495 root = iommu_phys_to_virt(*pte & PAGE_MASK);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003496
3497 level -= 1;
3498 }
3499
3500 return pte;
3501}
3502
3503static int __set_gcr3(struct protection_domain *domain, int pasid,
3504 unsigned long cr3)
3505{
3506 u64 *pte;
3507
3508 if (domain->mode != PAGE_MODE_NONE)
3509 return -EINVAL;
3510
3511 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3512 if (pte == NULL)
3513 return -ENOMEM;
3514
3515 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3516
3517 return __amd_iommu_flush_tlb(domain, pasid);
3518}
3519
3520static int __clear_gcr3(struct protection_domain *domain, int pasid)
3521{
3522 u64 *pte;
3523
3524 if (domain->mode != PAGE_MODE_NONE)
3525 return -EINVAL;
3526
3527 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3528 if (pte == NULL)
3529 return 0;
3530
3531 *pte = 0;
3532
3533 return __amd_iommu_flush_tlb(domain, pasid);
3534}
3535
3536int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3537 unsigned long cr3)
3538{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003539 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003540 unsigned long flags;
3541 int ret;
3542
3543 spin_lock_irqsave(&domain->lock, flags);
3544 ret = __set_gcr3(domain, pasid, cr3);
3545 spin_unlock_irqrestore(&domain->lock, flags);
3546
3547 return ret;
3548}
3549EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3550
3551int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3552{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003553 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003554 unsigned long flags;
3555 int ret;
3556
3557 spin_lock_irqsave(&domain->lock, flags);
3558 ret = __clear_gcr3(domain, pasid);
3559 spin_unlock_irqrestore(&domain->lock, flags);
3560
3561 return ret;
3562}
3563EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003564
3565int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3566 int status, int tag)
3567{
3568 struct iommu_dev_data *dev_data;
3569 struct amd_iommu *iommu;
3570 struct iommu_cmd cmd;
3571
3572 dev_data = get_dev_data(&pdev->dev);
3573 iommu = amd_iommu_rlookup_table[dev_data->devid];
3574
3575 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3576 tag, dev_data->pri_tlp);
3577
3578 return iommu_queue_command(iommu, &cmd);
3579}
3580EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003581
3582struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3583{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003584 struct protection_domain *pdomain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003585
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003586 pdomain = get_domain(&pdev->dev);
3587 if (IS_ERR(pdomain))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003588 return NULL;
3589
3590 /* Only return IOMMUv2 domains */
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003591 if (!(pdomain->flags & PD_IOMMUV2_MASK))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003592 return NULL;
3593
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003594 return &pdomain->domain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003595}
3596EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003597
3598void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3599{
3600 struct iommu_dev_data *dev_data;
3601
3602 if (!amd_iommu_v2_supported())
3603 return;
3604
3605 dev_data = get_dev_data(&pdev->dev);
3606 dev_data->errata |= (1 << erratum);
3607}
3608EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003609
3610int amd_iommu_device_info(struct pci_dev *pdev,
3611 struct amd_iommu_device_info *info)
3612{
3613 int max_pasids;
3614 int pos;
3615
3616 if (pdev == NULL || info == NULL)
3617 return -EINVAL;
3618
3619 if (!amd_iommu_v2_supported())
3620 return -EINVAL;
3621
3622 memset(info, 0, sizeof(*info));
3623
Gil Kupfercef74402018-05-10 17:56:02 -05003624 if (!pci_ats_disabled()) {
3625 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3626 if (pos)
3627 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3628 }
Joerg Roedel52efdb82011-12-07 12:01:36 +01003629
3630 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3631 if (pos)
3632 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3633
3634 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3635 if (pos) {
3636 int features;
3637
3638 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3639 max_pasids = min(max_pasids, (1 << 20));
3640
3641 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3642 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3643
3644 features = pci_pasid_features(pdev);
3645 if (features & PCI_PASID_CAP_EXEC)
3646 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3647 if (features & PCI_PASID_CAP_PRIV)
3648 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3649 }
3650
3651 return 0;
3652}
3653EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003654
3655#ifdef CONFIG_IRQ_REMAP
3656
3657/*****************************************************************************
3658 *
3659 * Interrupt Remapping Implementation
3660 *
3661 *****************************************************************************/
3662
Jiang Liu7c71d302015-04-13 14:11:33 +08003663static struct irq_chip amd_ir_chip;
Arnd Bergmann94c793a2018-04-04 12:56:59 +02003664static DEFINE_SPINLOCK(iommu_table_lock);
Jiang Liu7c71d302015-04-13 14:11:33 +08003665
Joerg Roedel2b324502012-06-21 16:29:10 +02003666static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3667{
3668 u64 dte;
3669
3670 dte = amd_iommu_dev_table[devid].data[2];
3671 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
Tom Lendacky2543a782017-07-17 16:10:24 -05003672 dte |= iommu_virt_to_phys(table->table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003673 dte |= DTE_IRQ_REMAP_INTCTL;
3674 dte |= DTE_IRQ_TABLE_LEN;
3675 dte |= DTE_IRQ_REMAP_ENABLE;
3676
3677 amd_iommu_dev_table[devid].data[2] = dte;
3678}
3679
Scott Wooddf42a042018-02-14 17:36:28 -06003680static struct irq_remap_table *get_irq_table(u16 devid)
3681{
3682 struct irq_remap_table *table;
3683
3684 if (WARN_ONCE(!amd_iommu_rlookup_table[devid],
3685 "%s: no iommu for devid %x\n", __func__, devid))
3686 return NULL;
3687
3688 table = irq_lookup_table[devid];
3689 if (WARN_ONCE(!table, "%s: no table for devid %x\n", __func__, devid))
3690 return NULL;
3691
3692 return table;
3693}
3694
Sebastian Andrzej Siewior993ca6e2018-03-22 16:22:40 +01003695static struct irq_remap_table *__alloc_irq_table(void)
3696{
3697 struct irq_remap_table *table;
3698
3699 table = kzalloc(sizeof(*table), GFP_KERNEL);
3700 if (!table)
3701 return NULL;
3702
3703 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_KERNEL);
3704 if (!table->table) {
3705 kfree(table);
3706 return NULL;
3707 }
3708 raw_spin_lock_init(&table->lock);
3709
3710 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3711 memset(table->table, 0,
3712 MAX_IRQS_PER_TABLE * sizeof(u32));
3713 else
3714 memset(table->table, 0,
3715 (MAX_IRQS_PER_TABLE * (sizeof(u64) * 2)));
3716 return table;
3717}
3718
Sebastian Andrzej Siewior2fcc1e82018-03-22 16:22:39 +01003719static void set_remap_table_entry(struct amd_iommu *iommu, u16 devid,
3720 struct irq_remap_table *table)
3721{
3722 irq_lookup_table[devid] = table;
3723 set_dte_irq_entry(devid, table);
3724 iommu_flush_dte(iommu, devid);
3725}
3726
Sebastian Andrzej Siewiorfde65dd2018-03-22 16:22:37 +01003727static struct irq_remap_table *alloc_irq_table(u16 devid)
Joerg Roedel2b324502012-06-21 16:29:10 +02003728{
3729 struct irq_remap_table *table = NULL;
Sebastian Andrzej Siewior993ca6e2018-03-22 16:22:40 +01003730 struct irq_remap_table *new_table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003731 struct amd_iommu *iommu;
3732 unsigned long flags;
3733 u16 alias;
3734
Sebastian Andrzej Siewiorea6166f2018-03-22 16:22:36 +01003735 spin_lock_irqsave(&iommu_table_lock, flags);
Joerg Roedel2b324502012-06-21 16:29:10 +02003736
3737 iommu = amd_iommu_rlookup_table[devid];
3738 if (!iommu)
3739 goto out_unlock;
3740
3741 table = irq_lookup_table[devid];
3742 if (table)
Baoquan He09284b92016-09-20 09:05:34 +08003743 goto out_unlock;
Joerg Roedel2b324502012-06-21 16:29:10 +02003744
3745 alias = amd_iommu_alias_table[devid];
3746 table = irq_lookup_table[alias];
3747 if (table) {
Sebastian Andrzej Siewior2fcc1e82018-03-22 16:22:39 +01003748 set_remap_table_entry(iommu, devid, table);
Sebastian Andrzej Siewior993ca6e2018-03-22 16:22:40 +01003749 goto out_wait;
Joerg Roedel2b324502012-06-21 16:29:10 +02003750 }
Sebastian Andrzej Siewior993ca6e2018-03-22 16:22:40 +01003751 spin_unlock_irqrestore(&iommu_table_lock, flags);
Joerg Roedel2b324502012-06-21 16:29:10 +02003752
3753 /* Nothing there yet, allocate new irq remapping table */
Sebastian Andrzej Siewior993ca6e2018-03-22 16:22:40 +01003754 new_table = __alloc_irq_table();
3755 if (!new_table)
3756 return NULL;
3757
3758 spin_lock_irqsave(&iommu_table_lock, flags);
3759
3760 table = irq_lookup_table[devid];
3761 if (table)
Baoquan He09284b92016-09-20 09:05:34 +08003762 goto out_unlock;
Joerg Roedel2b324502012-06-21 16:29:10 +02003763
Sebastian Andrzej Siewior993ca6e2018-03-22 16:22:40 +01003764 table = irq_lookup_table[alias];
3765 if (table) {
3766 set_remap_table_entry(iommu, devid, table);
3767 goto out_wait;
Joerg Roedel2b324502012-06-21 16:29:10 +02003768 }
3769
Sebastian Andrzej Siewior993ca6e2018-03-22 16:22:40 +01003770 table = new_table;
3771 new_table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003772
Sebastian Andrzej Siewior2fcc1e82018-03-22 16:22:39 +01003773 set_remap_table_entry(iommu, devid, table);
3774 if (devid != alias)
3775 set_remap_table_entry(iommu, alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003776
Sebastian Andrzej Siewior993ca6e2018-03-22 16:22:40 +01003777out_wait:
Joerg Roedel2b324502012-06-21 16:29:10 +02003778 iommu_completion_wait(iommu);
3779
3780out_unlock:
Sebastian Andrzej Siewiorea6166f2018-03-22 16:22:36 +01003781 spin_unlock_irqrestore(&iommu_table_lock, flags);
Joerg Roedel2b324502012-06-21 16:29:10 +02003782
Sebastian Andrzej Siewior993ca6e2018-03-22 16:22:40 +01003783 if (new_table) {
3784 kmem_cache_free(amd_iommu_irq_cache, new_table->table);
3785 kfree(new_table);
3786 }
Joerg Roedel2b324502012-06-21 16:29:10 +02003787 return table;
3788}
3789
Joerg Roedel37946d92017-10-06 12:16:39 +02003790static int alloc_irq_index(u16 devid, int count, bool align)
Joerg Roedel2b324502012-06-21 16:29:10 +02003791{
3792 struct irq_remap_table *table;
Joerg Roedel37946d92017-10-06 12:16:39 +02003793 int index, c, alignment = 1;
Joerg Roedel2b324502012-06-21 16:29:10 +02003794 unsigned long flags;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003795 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
3796
3797 if (!iommu)
3798 return -ENODEV;
Joerg Roedel2b324502012-06-21 16:29:10 +02003799
Sebastian Andrzej Siewiorfde65dd2018-03-22 16:22:37 +01003800 table = alloc_irq_table(devid);
Joerg Roedel2b324502012-06-21 16:29:10 +02003801 if (!table)
3802 return -ENODEV;
3803
Joerg Roedel37946d92017-10-06 12:16:39 +02003804 if (align)
3805 alignment = roundup_pow_of_two(count);
3806
Scott Wood27790392018-01-21 03:28:54 -06003807 raw_spin_lock_irqsave(&table->lock, flags);
Joerg Roedel2b324502012-06-21 16:29:10 +02003808
3809 /* Scan table for free entries */
Joerg Roedel37946d92017-10-06 12:16:39 +02003810 for (index = ALIGN(table->min_index, alignment), c = 0;
Alex Williamson07d1c912017-11-03 10:50:31 -06003811 index < MAX_IRQS_PER_TABLE;) {
Joerg Roedel37946d92017-10-06 12:16:39 +02003812 if (!iommu->irte_ops->is_allocated(table, index)) {
Joerg Roedel2b324502012-06-21 16:29:10 +02003813 c += 1;
Joerg Roedel37946d92017-10-06 12:16:39 +02003814 } else {
3815 c = 0;
Alex Williamson07d1c912017-11-03 10:50:31 -06003816 index = ALIGN(index + 1, alignment);
Joerg Roedel37946d92017-10-06 12:16:39 +02003817 continue;
3818 }
Joerg Roedel2b324502012-06-21 16:29:10 +02003819
3820 if (c == count) {
Joerg Roedel2b324502012-06-21 16:29:10 +02003821 for (; c != 0; --c)
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003822 iommu->irte_ops->set_allocated(table, index - c + 1);
Joerg Roedel2b324502012-06-21 16:29:10 +02003823
3824 index -= count - 1;
Joerg Roedel2b324502012-06-21 16:29:10 +02003825 goto out;
3826 }
Alex Williamson07d1c912017-11-03 10:50:31 -06003827
3828 index++;
Joerg Roedel2b324502012-06-21 16:29:10 +02003829 }
3830
3831 index = -ENOSPC;
3832
3833out:
Scott Wood27790392018-01-21 03:28:54 -06003834 raw_spin_unlock_irqrestore(&table->lock, flags);
Joerg Roedel2b324502012-06-21 16:29:10 +02003835
3836 return index;
3837}
3838
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05003839static int modify_irte_ga(u16 devid, int index, struct irte_ga *irte,
3840 struct amd_ir_data *data)
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003841{
3842 struct irq_remap_table *table;
3843 struct amd_iommu *iommu;
3844 unsigned long flags;
3845 struct irte_ga *entry;
3846
3847 iommu = amd_iommu_rlookup_table[devid];
3848 if (iommu == NULL)
3849 return -EINVAL;
3850
Scott Wooddf42a042018-02-14 17:36:28 -06003851 table = get_irq_table(devid);
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003852 if (!table)
3853 return -ENOMEM;
3854
Scott Wood27790392018-01-21 03:28:54 -06003855 raw_spin_lock_irqsave(&table->lock, flags);
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003856
3857 entry = (struct irte_ga *)table->table;
3858 entry = &entry[index];
3859 entry->lo.fields_remap.valid = 0;
3860 entry->hi.val = irte->hi.val;
3861 entry->lo.val = irte->lo.val;
3862 entry->lo.fields_remap.valid = 1;
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05003863 if (data)
3864 data->ref = entry;
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003865
Scott Wood27790392018-01-21 03:28:54 -06003866 raw_spin_unlock_irqrestore(&table->lock, flags);
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003867
3868 iommu_flush_irt(iommu, devid);
3869 iommu_completion_wait(iommu);
3870
3871 return 0;
3872}
3873
3874static int modify_irte(u16 devid, int index, union irte *irte)
Joerg Roedel2b324502012-06-21 16:29:10 +02003875{
3876 struct irq_remap_table *table;
3877 struct amd_iommu *iommu;
3878 unsigned long flags;
3879
3880 iommu = amd_iommu_rlookup_table[devid];
3881 if (iommu == NULL)
3882 return -EINVAL;
3883
Scott Wooddf42a042018-02-14 17:36:28 -06003884 table = get_irq_table(devid);
Joerg Roedel2b324502012-06-21 16:29:10 +02003885 if (!table)
3886 return -ENOMEM;
3887
Scott Wood27790392018-01-21 03:28:54 -06003888 raw_spin_lock_irqsave(&table->lock, flags);
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003889 table->table[index] = irte->val;
Scott Wood27790392018-01-21 03:28:54 -06003890 raw_spin_unlock_irqrestore(&table->lock, flags);
Joerg Roedel2b324502012-06-21 16:29:10 +02003891
3892 iommu_flush_irt(iommu, devid);
3893 iommu_completion_wait(iommu);
3894
3895 return 0;
3896}
3897
3898static void free_irte(u16 devid, int index)
3899{
3900 struct irq_remap_table *table;
3901 struct amd_iommu *iommu;
3902 unsigned long flags;
3903
3904 iommu = amd_iommu_rlookup_table[devid];
3905 if (iommu == NULL)
3906 return;
3907
Scott Wooddf42a042018-02-14 17:36:28 -06003908 table = get_irq_table(devid);
Joerg Roedel2b324502012-06-21 16:29:10 +02003909 if (!table)
3910 return;
3911
Scott Wood27790392018-01-21 03:28:54 -06003912 raw_spin_lock_irqsave(&table->lock, flags);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003913 iommu->irte_ops->clear_allocated(table, index);
Scott Wood27790392018-01-21 03:28:54 -06003914 raw_spin_unlock_irqrestore(&table->lock, flags);
Joerg Roedel2b324502012-06-21 16:29:10 +02003915
3916 iommu_flush_irt(iommu, devid);
3917 iommu_completion_wait(iommu);
3918}
3919
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003920static void irte_prepare(void *entry,
3921 u32 delivery_mode, u32 dest_mode,
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05003922 u8 vector, u32 dest_apicid, int devid)
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003923{
3924 union irte *irte = (union irte *) entry;
3925
3926 irte->val = 0;
3927 irte->fields.vector = vector;
3928 irte->fields.int_type = delivery_mode;
3929 irte->fields.destination = dest_apicid;
3930 irte->fields.dm = dest_mode;
3931 irte->fields.valid = 1;
3932}
3933
3934static void irte_ga_prepare(void *entry,
3935 u32 delivery_mode, u32 dest_mode,
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05003936 u8 vector, u32 dest_apicid, int devid)
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003937{
3938 struct irte_ga *irte = (struct irte_ga *) entry;
3939
3940 irte->lo.val = 0;
3941 irte->hi.val = 0;
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003942 irte->lo.fields_remap.int_type = delivery_mode;
3943 irte->lo.fields_remap.dm = dest_mode;
3944 irte->hi.fields.vector = vector;
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05003945 irte->lo.fields_remap.destination = APICID_TO_IRTE_DEST_LO(dest_apicid);
3946 irte->hi.fields.destination = APICID_TO_IRTE_DEST_HI(dest_apicid);
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003947 irte->lo.fields_remap.valid = 1;
3948}
3949
3950static void irte_activate(void *entry, u16 devid, u16 index)
3951{
3952 union irte *irte = (union irte *) entry;
3953
3954 irte->fields.valid = 1;
3955 modify_irte(devid, index, irte);
3956}
3957
3958static void irte_ga_activate(void *entry, u16 devid, u16 index)
3959{
3960 struct irte_ga *irte = (struct irte_ga *) entry;
3961
3962 irte->lo.fields_remap.valid = 1;
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05003963 modify_irte_ga(devid, index, irte, NULL);
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003964}
3965
3966static void irte_deactivate(void *entry, u16 devid, u16 index)
3967{
3968 union irte *irte = (union irte *) entry;
3969
3970 irte->fields.valid = 0;
3971 modify_irte(devid, index, irte);
3972}
3973
3974static void irte_ga_deactivate(void *entry, u16 devid, u16 index)
3975{
3976 struct irte_ga *irte = (struct irte_ga *) entry;
3977
3978 irte->lo.fields_remap.valid = 0;
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05003979 modify_irte_ga(devid, index, irte, NULL);
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003980}
3981
3982static void irte_set_affinity(void *entry, u16 devid, u16 index,
3983 u8 vector, u32 dest_apicid)
3984{
3985 union irte *irte = (union irte *) entry;
3986
3987 irte->fields.vector = vector;
3988 irte->fields.destination = dest_apicid;
3989 modify_irte(devid, index, irte);
3990}
3991
3992static void irte_ga_set_affinity(void *entry, u16 devid, u16 index,
3993 u8 vector, u32 dest_apicid)
3994{
3995 struct irte_ga *irte = (struct irte_ga *) entry;
3996
Scott Wood01ee04b2018-01-28 14:22:19 -06003997 if (!irte->lo.fields_remap.guest_mode) {
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05003998 irte->hi.fields.vector = vector;
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05003999 irte->lo.fields_remap.destination =
4000 APICID_TO_IRTE_DEST_LO(dest_apicid);
4001 irte->hi.fields.destination =
4002 APICID_TO_IRTE_DEST_HI(dest_apicid);
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05004003 modify_irte_ga(devid, index, irte, NULL);
4004 }
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05004005}
4006
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004007#define IRTE_ALLOCATED (~1U)
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05004008static void irte_set_allocated(struct irq_remap_table *table, int index)
4009{
4010 table->table[index] = IRTE_ALLOCATED;
4011}
4012
4013static void irte_ga_set_allocated(struct irq_remap_table *table, int index)
4014{
4015 struct irte_ga *ptr = (struct irte_ga *)table->table;
4016 struct irte_ga *irte = &ptr[index];
4017
4018 memset(&irte->lo.val, 0, sizeof(u64));
4019 memset(&irte->hi.val, 0, sizeof(u64));
4020 irte->hi.fields.vector = 0xff;
4021}
4022
4023static bool irte_is_allocated(struct irq_remap_table *table, int index)
4024{
4025 union irte *ptr = (union irte *)table->table;
4026 union irte *irte = &ptr[index];
4027
4028 return irte->val != 0;
4029}
4030
4031static bool irte_ga_is_allocated(struct irq_remap_table *table, int index)
4032{
4033 struct irte_ga *ptr = (struct irte_ga *)table->table;
4034 struct irte_ga *irte = &ptr[index];
4035
4036 return irte->hi.fields.vector != 0;
4037}
4038
4039static void irte_clear_allocated(struct irq_remap_table *table, int index)
4040{
4041 table->table[index] = 0;
4042}
4043
4044static void irte_ga_clear_allocated(struct irq_remap_table *table, int index)
4045{
4046 struct irte_ga *ptr = (struct irte_ga *)table->table;
4047 struct irte_ga *irte = &ptr[index];
4048
4049 memset(&irte->lo.val, 0, sizeof(u64));
4050 memset(&irte->hi.val, 0, sizeof(u64));
4051}
4052
Jiang Liu7c71d302015-04-13 14:11:33 +08004053static int get_devid(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02004054{
Jiang Liu7c71d302015-04-13 14:11:33 +08004055 int devid = -1;
Joerg Roedel5527de72012-06-26 11:17:32 +02004056
Jiang Liu7c71d302015-04-13 14:11:33 +08004057 switch (info->type) {
4058 case X86_IRQ_ALLOC_TYPE_IOAPIC:
4059 devid = get_ioapic_devid(info->ioapic_id);
4060 break;
4061 case X86_IRQ_ALLOC_TYPE_HPET:
4062 devid = get_hpet_devid(info->hpet_id);
4063 break;
4064 case X86_IRQ_ALLOC_TYPE_MSI:
4065 case X86_IRQ_ALLOC_TYPE_MSIX:
4066 devid = get_device_id(&info->msi_dev->dev);
4067 break;
4068 default:
4069 BUG_ON(1);
4070 break;
Joerg Roedel5527de72012-06-26 11:17:32 +02004071 }
4072
Jiang Liu7c71d302015-04-13 14:11:33 +08004073 return devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02004074}
4075
Jiang Liu7c71d302015-04-13 14:11:33 +08004076static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02004077{
Jiang Liu7c71d302015-04-13 14:11:33 +08004078 struct amd_iommu *iommu;
4079 int devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02004080
Jiang Liu7c71d302015-04-13 14:11:33 +08004081 if (!info)
4082 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02004083
Jiang Liu7c71d302015-04-13 14:11:33 +08004084 devid = get_devid(info);
4085 if (devid >= 0) {
4086 iommu = amd_iommu_rlookup_table[devid];
4087 if (iommu)
4088 return iommu->ir_domain;
4089 }
Joerg Roedel5527de72012-06-26 11:17:32 +02004090
Jiang Liu7c71d302015-04-13 14:11:33 +08004091 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02004092}
4093
Jiang Liu7c71d302015-04-13 14:11:33 +08004094static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004095{
Jiang Liu7c71d302015-04-13 14:11:33 +08004096 struct amd_iommu *iommu;
4097 int devid;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004098
Jiang Liu7c71d302015-04-13 14:11:33 +08004099 if (!info)
4100 return NULL;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004101
Jiang Liu7c71d302015-04-13 14:11:33 +08004102 switch (info->type) {
4103 case X86_IRQ_ALLOC_TYPE_MSI:
4104 case X86_IRQ_ALLOC_TYPE_MSIX:
4105 devid = get_device_id(&info->msi_dev->dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02004106 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04004107 return NULL;
4108
Dan Carpenter1fb260b2016-01-07 12:36:06 +03004109 iommu = amd_iommu_rlookup_table[devid];
4110 if (iommu)
4111 return iommu->msi_domain;
Jiang Liu7c71d302015-04-13 14:11:33 +08004112 break;
4113 default:
4114 break;
4115 }
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004116
Jiang Liu7c71d302015-04-13 14:11:33 +08004117 return NULL;
Joerg Roedeld9761952012-06-26 16:00:08 +02004118}
4119
Joerg Roedel6b474b82012-06-26 16:46:04 +02004120struct irq_remap_ops amd_iommu_irq_ops = {
Joerg Roedel6b474b82012-06-26 16:46:04 +02004121 .prepare = amd_iommu_prepare,
4122 .enable = amd_iommu_enable,
4123 .disable = amd_iommu_disable,
4124 .reenable = amd_iommu_reenable,
4125 .enable_faulting = amd_iommu_enable_faulting,
Jiang Liu7c71d302015-04-13 14:11:33 +08004126 .get_ir_irq_domain = get_ir_irq_domain,
4127 .get_irq_domain = get_irq_domain,
Joerg Roedel6b474b82012-06-26 16:46:04 +02004128};
Jiang Liu7c71d302015-04-13 14:11:33 +08004129
4130static void irq_remapping_prepare_irte(struct amd_ir_data *data,
4131 struct irq_cfg *irq_cfg,
4132 struct irq_alloc_info *info,
4133 int devid, int index, int sub_handle)
4134{
4135 struct irq_2_irte *irte_info = &data->irq_2_irte;
4136 struct msi_msg *msg = &data->msi_entry;
Jiang Liu7c71d302015-04-13 14:11:33 +08004137 struct IO_APIC_route_entry *entry;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004138 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
4139
4140 if (!iommu)
4141 return;
Jiang Liu7c71d302015-04-13 14:11:33 +08004142
Jiang Liu7c71d302015-04-13 14:11:33 +08004143 data->irq_2_irte.devid = devid;
4144 data->irq_2_irte.index = index + sub_handle;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004145 iommu->irte_ops->prepare(data->entry, apic->irq_delivery_mode,
4146 apic->irq_dest_mode, irq_cfg->vector,
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05004147 irq_cfg->dest_apicid, devid);
Jiang Liu7c71d302015-04-13 14:11:33 +08004148
4149 switch (info->type) {
4150 case X86_IRQ_ALLOC_TYPE_IOAPIC:
4151 /* Setup IOAPIC entry */
4152 entry = info->ioapic_entry;
4153 info->ioapic_entry = NULL;
4154 memset(entry, 0, sizeof(*entry));
4155 entry->vector = index;
4156 entry->mask = 0;
4157 entry->trigger = info->ioapic_trigger;
4158 entry->polarity = info->ioapic_polarity;
4159 /* Mask level triggered irqs. */
4160 if (info->ioapic_trigger)
4161 entry->mask = 1;
4162 break;
4163
4164 case X86_IRQ_ALLOC_TYPE_HPET:
4165 case X86_IRQ_ALLOC_TYPE_MSI:
4166 case X86_IRQ_ALLOC_TYPE_MSIX:
4167 msg->address_hi = MSI_ADDR_BASE_HI;
4168 msg->address_lo = MSI_ADDR_BASE_LO;
4169 msg->data = irte_info->index;
4170 break;
4171
4172 default:
4173 BUG_ON(1);
4174 break;
4175 }
4176}
4177
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05004178struct amd_irte_ops irte_32_ops = {
4179 .prepare = irte_prepare,
4180 .activate = irte_activate,
4181 .deactivate = irte_deactivate,
4182 .set_affinity = irte_set_affinity,
4183 .set_allocated = irte_set_allocated,
4184 .is_allocated = irte_is_allocated,
4185 .clear_allocated = irte_clear_allocated,
4186};
4187
4188struct amd_irte_ops irte_128_ops = {
4189 .prepare = irte_ga_prepare,
4190 .activate = irte_ga_activate,
4191 .deactivate = irte_ga_deactivate,
4192 .set_affinity = irte_ga_set_affinity,
4193 .set_allocated = irte_ga_set_allocated,
4194 .is_allocated = irte_ga_is_allocated,
4195 .clear_allocated = irte_ga_clear_allocated,
4196};
4197
Jiang Liu7c71d302015-04-13 14:11:33 +08004198static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
4199 unsigned int nr_irqs, void *arg)
4200{
4201 struct irq_alloc_info *info = arg;
4202 struct irq_data *irq_data;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004203 struct amd_ir_data *data = NULL;
Jiang Liu7c71d302015-04-13 14:11:33 +08004204 struct irq_cfg *cfg;
4205 int i, ret, devid;
Sebastian Andrzej Siewior29d049b2018-03-22 16:22:42 +01004206 int index;
Jiang Liu7c71d302015-04-13 14:11:33 +08004207
4208 if (!info)
4209 return -EINVAL;
4210 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
4211 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
4212 return -EINVAL;
4213
4214 /*
4215 * With IRQ remapping enabled, don't need contiguous CPU vectors
4216 * to support multiple MSI interrupts.
4217 */
4218 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
4219 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
4220
4221 devid = get_devid(info);
4222 if (devid < 0)
4223 return -EINVAL;
4224
4225 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
4226 if (ret < 0)
4227 return ret;
4228
Jiang Liu7c71d302015-04-13 14:11:33 +08004229 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
Sebastian Andrzej Siewiorfde65dd2018-03-22 16:22:37 +01004230 struct irq_remap_table *table;
4231 struct amd_iommu *iommu;
4232
4233 table = alloc_irq_table(devid);
4234 if (table) {
4235 if (!table->min_index) {
4236 /*
4237 * Keep the first 32 indexes free for IOAPIC
4238 * interrupts.
4239 */
4240 table->min_index = 32;
4241 iommu = amd_iommu_rlookup_table[devid];
4242 for (i = 0; i < 32; ++i)
4243 iommu->irte_ops->set_allocated(table, i);
4244 }
4245 WARN_ON(table->min_index != 32);
Jiang Liu7c71d302015-04-13 14:11:33 +08004246 index = info->ioapic_pin;
Sebastian Andrzej Siewiorfde65dd2018-03-22 16:22:37 +01004247 } else {
Sebastian Andrzej Siewior29d049b2018-03-22 16:22:42 +01004248 index = -ENOMEM;
Sebastian Andrzej Siewiorfde65dd2018-03-22 16:22:37 +01004249 }
Jiang Liu7c71d302015-04-13 14:11:33 +08004250 } else {
Joerg Roedel53b9ec32017-10-06 12:22:06 +02004251 bool align = (info->type == X86_IRQ_ALLOC_TYPE_MSI);
4252
4253 index = alloc_irq_index(devid, nr_irqs, align);
Jiang Liu7c71d302015-04-13 14:11:33 +08004254 }
4255 if (index < 0) {
4256 pr_warn("Failed to allocate IRTE\n");
Wei Yongjun517abe42016-07-28 02:10:26 +00004257 ret = index;
Jiang Liu7c71d302015-04-13 14:11:33 +08004258 goto out_free_parent;
4259 }
4260
4261 for (i = 0; i < nr_irqs; i++) {
4262 irq_data = irq_domain_get_irq_data(domain, virq + i);
4263 cfg = irqd_cfg(irq_data);
4264 if (!irq_data || !cfg) {
4265 ret = -EINVAL;
4266 goto out_free_data;
4267 }
4268
Joerg Roedela130e692015-08-13 11:07:25 +02004269 ret = -ENOMEM;
4270 data = kzalloc(sizeof(*data), GFP_KERNEL);
4271 if (!data)
4272 goto out_free_data;
4273
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004274 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
4275 data->entry = kzalloc(sizeof(union irte), GFP_KERNEL);
4276 else
4277 data->entry = kzalloc(sizeof(struct irte_ga),
4278 GFP_KERNEL);
4279 if (!data->entry) {
4280 kfree(data);
4281 goto out_free_data;
4282 }
4283
Jiang Liu7c71d302015-04-13 14:11:33 +08004284 irq_data->hwirq = (devid << 16) + i;
4285 irq_data->chip_data = data;
4286 irq_data->chip = &amd_ir_chip;
4287 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
4288 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
4289 }
Joerg Roedela130e692015-08-13 11:07:25 +02004290
Jiang Liu7c71d302015-04-13 14:11:33 +08004291 return 0;
4292
4293out_free_data:
4294 for (i--; i >= 0; i--) {
4295 irq_data = irq_domain_get_irq_data(domain, virq + i);
4296 if (irq_data)
4297 kfree(irq_data->chip_data);
4298 }
4299 for (i = 0; i < nr_irqs; i++)
4300 free_irte(devid, index + i);
4301out_free_parent:
4302 irq_domain_free_irqs_common(domain, virq, nr_irqs);
4303 return ret;
4304}
4305
4306static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
4307 unsigned int nr_irqs)
4308{
4309 struct irq_2_irte *irte_info;
4310 struct irq_data *irq_data;
4311 struct amd_ir_data *data;
4312 int i;
4313
4314 for (i = 0; i < nr_irqs; i++) {
4315 irq_data = irq_domain_get_irq_data(domain, virq + i);
4316 if (irq_data && irq_data->chip_data) {
4317 data = irq_data->chip_data;
4318 irte_info = &data->irq_2_irte;
4319 free_irte(irte_info->devid, irte_info->index);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004320 kfree(data->entry);
Jiang Liu7c71d302015-04-13 14:11:33 +08004321 kfree(data);
4322 }
4323 }
4324 irq_domain_free_irqs_common(domain, virq, nr_irqs);
4325}
4326
Thomas Gleixner5ba204a2017-09-13 23:29:48 +02004327static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
4328 struct amd_ir_data *ir_data,
4329 struct irq_2_irte *irte_info,
4330 struct irq_cfg *cfg);
4331
Thomas Gleixner72491642017-09-13 23:29:10 +02004332static int irq_remapping_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01004333 struct irq_data *irq_data, bool reserve)
Jiang Liu7c71d302015-04-13 14:11:33 +08004334{
4335 struct amd_ir_data *data = irq_data->chip_data;
4336 struct irq_2_irte *irte_info = &data->irq_2_irte;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004337 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
Thomas Gleixner5ba204a2017-09-13 23:29:48 +02004338 struct irq_cfg *cfg = irqd_cfg(irq_data);
Jiang Liu7c71d302015-04-13 14:11:33 +08004339
Thomas Gleixner5ba204a2017-09-13 23:29:48 +02004340 if (!iommu)
4341 return 0;
4342
4343 iommu->irte_ops->activate(data->entry, irte_info->devid,
4344 irte_info->index);
4345 amd_ir_update_irte(irq_data, iommu, data, irte_info, cfg);
Thomas Gleixner72491642017-09-13 23:29:10 +02004346 return 0;
Jiang Liu7c71d302015-04-13 14:11:33 +08004347}
4348
4349static void irq_remapping_deactivate(struct irq_domain *domain,
4350 struct irq_data *irq_data)
4351{
4352 struct amd_ir_data *data = irq_data->chip_data;
4353 struct irq_2_irte *irte_info = &data->irq_2_irte;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004354 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
Jiang Liu7c71d302015-04-13 14:11:33 +08004355
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004356 if (iommu)
4357 iommu->irte_ops->deactivate(data->entry, irte_info->devid,
4358 irte_info->index);
Jiang Liu7c71d302015-04-13 14:11:33 +08004359}
4360
Tobias Klausere2f9d452017-05-24 16:31:16 +02004361static const struct irq_domain_ops amd_ir_domain_ops = {
Jiang Liu7c71d302015-04-13 14:11:33 +08004362 .alloc = irq_remapping_alloc,
4363 .free = irq_remapping_free,
4364 .activate = irq_remapping_activate,
4365 .deactivate = irq_remapping_deactivate,
4366};
4367
Suthikulpanit, Suraveeb9c6ff92019-07-23 19:00:37 +00004368int amd_iommu_activate_guest_mode(void *data)
4369{
4370 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
4371 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
4372
4373 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
4374 !entry || entry->lo.fields_vapic.guest_mode)
4375 return 0;
4376
4377 entry->lo.val = 0;
4378 entry->hi.val = 0;
4379
4380 entry->lo.fields_vapic.guest_mode = 1;
4381 entry->lo.fields_vapic.ga_log_intr = 1;
4382 entry->hi.fields.ga_root_ptr = ir_data->ga_root_ptr;
4383 entry->hi.fields.vector = ir_data->ga_vector;
4384 entry->lo.fields_vapic.ga_tag = ir_data->ga_tag;
4385
4386 return modify_irte_ga(ir_data->irq_2_irte.devid,
4387 ir_data->irq_2_irte.index, entry, NULL);
4388}
4389EXPORT_SYMBOL(amd_iommu_activate_guest_mode);
4390
4391int amd_iommu_deactivate_guest_mode(void *data)
4392{
4393 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
4394 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
4395 struct irq_cfg *cfg = ir_data->cfg;
4396
4397 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
4398 !entry || !entry->lo.fields_vapic.guest_mode)
4399 return 0;
4400
4401 entry->lo.val = 0;
4402 entry->hi.val = 0;
4403
4404 entry->lo.fields_remap.dm = apic->irq_dest_mode;
4405 entry->lo.fields_remap.int_type = apic->irq_delivery_mode;
4406 entry->hi.fields.vector = cfg->vector;
4407 entry->lo.fields_remap.destination =
4408 APICID_TO_IRTE_DEST_LO(cfg->dest_apicid);
4409 entry->hi.fields.destination =
4410 APICID_TO_IRTE_DEST_HI(cfg->dest_apicid);
4411
4412 return modify_irte_ga(ir_data->irq_2_irte.devid,
4413 ir_data->irq_2_irte.index, entry, NULL);
4414}
4415EXPORT_SYMBOL(amd_iommu_deactivate_guest_mode);
4416
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05004417static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)
4418{
Suthikulpanit, Suraveeb9c6ff92019-07-23 19:00:37 +00004419 int ret;
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05004420 struct amd_iommu *iommu;
4421 struct amd_iommu_pi_data *pi_data = vcpu_info;
4422 struct vcpu_data *vcpu_pi_info = pi_data->vcpu_data;
4423 struct amd_ir_data *ir_data = data->chip_data;
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05004424 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05004425 struct iommu_dev_data *dev_data = search_dev_data(irte_info->devid);
4426
4427 /* Note:
4428 * This device has never been set up for guest mode.
4429 * we should not modify the IRTE
4430 */
4431 if (!dev_data || !dev_data->use_vapic)
4432 return 0;
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05004433
Suthikulpanit, Suraveeb9c6ff92019-07-23 19:00:37 +00004434 ir_data->cfg = irqd_cfg(data);
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05004435 pi_data->ir_data = ir_data;
4436
4437 /* Note:
4438 * SVM tries to set up for VAPIC mode, but we are in
4439 * legacy mode. So, we force legacy mode instead.
4440 */
4441 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
Joerg Roedel101fa032018-11-27 16:22:31 +01004442 pr_debug("%s: Fall back to using intr legacy remap\n",
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05004443 __func__);
4444 pi_data->is_guest_mode = false;
4445 }
4446
4447 iommu = amd_iommu_rlookup_table[irte_info->devid];
4448 if (iommu == NULL)
4449 return -EINVAL;
4450
4451 pi_data->prev_ga_tag = ir_data->cached_ga_tag;
4452 if (pi_data->is_guest_mode) {
Suthikulpanit, Suraveeb9c6ff92019-07-23 19:00:37 +00004453 ir_data->ga_root_ptr = (pi_data->base >> 12);
4454 ir_data->ga_vector = vcpu_pi_info->vector;
4455 ir_data->ga_tag = pi_data->ga_tag;
4456 ret = amd_iommu_activate_guest_mode(ir_data);
4457 if (!ret)
4458 ir_data->cached_ga_tag = pi_data->ga_tag;
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05004459 } else {
Suthikulpanit, Suraveeb9c6ff92019-07-23 19:00:37 +00004460 ret = amd_iommu_deactivate_guest_mode(ir_data);
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05004461
4462 /*
4463 * This communicates the ga_tag back to the caller
4464 * so that it can do all the necessary clean up.
4465 */
Suthikulpanit, Suraveeb9c6ff92019-07-23 19:00:37 +00004466 if (!ret)
4467 ir_data->cached_ga_tag = 0;
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05004468 }
4469
Suthikulpanit, Suraveeb9c6ff92019-07-23 19:00:37 +00004470 return ret;
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05004471}
4472
Thomas Gleixner5ba204a2017-09-13 23:29:48 +02004473
4474static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
4475 struct amd_ir_data *ir_data,
4476 struct irq_2_irte *irte_info,
4477 struct irq_cfg *cfg)
4478{
4479
4480 /*
4481 * Atomically updates the IRTE with the new destination, vector
4482 * and flushes the interrupt entry cache.
4483 */
4484 iommu->irte_ops->set_affinity(ir_data->entry, irte_info->devid,
4485 irte_info->index, cfg->vector,
4486 cfg->dest_apicid);
4487}
4488
Jiang Liu7c71d302015-04-13 14:11:33 +08004489static int amd_ir_set_affinity(struct irq_data *data,
4490 const struct cpumask *mask, bool force)
4491{
4492 struct amd_ir_data *ir_data = data->chip_data;
4493 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
4494 struct irq_cfg *cfg = irqd_cfg(data);
4495 struct irq_data *parent = data->parent_data;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004496 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
Jiang Liu7c71d302015-04-13 14:11:33 +08004497 int ret;
4498
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004499 if (!iommu)
4500 return -ENODEV;
4501
Jiang Liu7c71d302015-04-13 14:11:33 +08004502 ret = parent->chip->irq_set_affinity(parent, mask, force);
4503 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
4504 return ret;
4505
Thomas Gleixner5ba204a2017-09-13 23:29:48 +02004506 amd_ir_update_irte(data, iommu, ir_data, irte_info, cfg);
Jiang Liu7c71d302015-04-13 14:11:33 +08004507 /*
4508 * After this point, all the interrupts will start arriving
4509 * at the new destination. So, time to cleanup the previous
4510 * vector allocation.
4511 */
Jiang Liuc6c20022015-04-14 10:30:02 +08004512 send_cleanup_vector(cfg);
Jiang Liu7c71d302015-04-13 14:11:33 +08004513
4514 return IRQ_SET_MASK_OK_DONE;
4515}
4516
4517static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
4518{
4519 struct amd_ir_data *ir_data = irq_data->chip_data;
4520
4521 *msg = ir_data->msi_entry;
4522}
4523
4524static struct irq_chip amd_ir_chip = {
Thomas Gleixner290be192017-06-20 01:37:02 +02004525 .name = "AMD-IR",
Thomas Gleixner8a2b7d12018-06-04 17:33:56 +02004526 .irq_ack = apic_ack_irq,
Thomas Gleixner290be192017-06-20 01:37:02 +02004527 .irq_set_affinity = amd_ir_set_affinity,
4528 .irq_set_vcpu_affinity = amd_ir_set_vcpu_affinity,
4529 .irq_compose_msi_msg = ir_compose_msi_msg,
Jiang Liu7c71d302015-04-13 14:11:33 +08004530};
4531
4532int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
4533{
Thomas Gleixner3e49a812017-06-20 01:37:12 +02004534 struct fwnode_handle *fn;
4535
4536 fn = irq_domain_alloc_named_id_fwnode("AMD-IR", iommu->index);
4537 if (!fn)
4538 return -ENOMEM;
4539 iommu->ir_domain = irq_domain_create_tree(fn, &amd_ir_domain_ops, iommu);
4540 irq_domain_free_fwnode(fn);
Jiang Liu7c71d302015-04-13 14:11:33 +08004541 if (!iommu->ir_domain)
4542 return -ENOMEM;
4543
4544 iommu->ir_domain->parent = arch_get_ir_parent_domain();
Thomas Gleixner3e49a812017-06-20 01:37:12 +02004545 iommu->msi_domain = arch_create_remap_msi_irq_domain(iommu->ir_domain,
4546 "AMD-IR-MSI",
4547 iommu->index);
Jiang Liu7c71d302015-04-13 14:11:33 +08004548 return 0;
4549}
Suravee Suthikulpanit8dbea3f2016-08-23 13:52:38 -05004550
4551int amd_iommu_update_ga(int cpu, bool is_run, void *data)
4552{
4553 unsigned long flags;
4554 struct amd_iommu *iommu;
Sebastian Andrzej Siewior4fde5412018-03-22 16:22:38 +01004555 struct irq_remap_table *table;
Suravee Suthikulpanit8dbea3f2016-08-23 13:52:38 -05004556 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
4557 int devid = ir_data->irq_2_irte.devid;
4558 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
4559 struct irte_ga *ref = (struct irte_ga *) ir_data->ref;
4560
4561 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
4562 !ref || !entry || !entry->lo.fields_vapic.guest_mode)
4563 return 0;
4564
4565 iommu = amd_iommu_rlookup_table[devid];
4566 if (!iommu)
4567 return -ENODEV;
4568
Sebastian Andrzej Siewior4fde5412018-03-22 16:22:38 +01004569 table = get_irq_table(devid);
4570 if (!table)
Suravee Suthikulpanit8dbea3f2016-08-23 13:52:38 -05004571 return -ENODEV;
4572
Sebastian Andrzej Siewior4fde5412018-03-22 16:22:38 +01004573 raw_spin_lock_irqsave(&table->lock, flags);
Suravee Suthikulpanit8dbea3f2016-08-23 13:52:38 -05004574
4575 if (ref->lo.fields_vapic.guest_mode) {
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05004576 if (cpu >= 0) {
4577 ref->lo.fields_vapic.destination =
4578 APICID_TO_IRTE_DEST_LO(cpu);
4579 ref->hi.fields.destination =
4580 APICID_TO_IRTE_DEST_HI(cpu);
4581 }
Suravee Suthikulpanit8dbea3f2016-08-23 13:52:38 -05004582 ref->lo.fields_vapic.is_run = is_run;
4583 barrier();
4584 }
4585
Sebastian Andrzej Siewior4fde5412018-03-22 16:22:38 +01004586 raw_spin_unlock_irqrestore(&table->lock, flags);
Suravee Suthikulpanit8dbea3f2016-08-23 13:52:38 -05004587
4588 iommu_flush_irt(iommu, devid);
4589 iommu_completion_wait(iommu);
4590 return 0;
4591}
4592EXPORT_SYMBOL(amd_iommu_update_ga);
Joerg Roedel2b324502012-06-21 16:29:10 +02004593#endif