blob: 400867f01d35cb2e1bcf7bec77365da40dde0ea5 [file] [log] [blame]
Joerg Roedelb6c02712008-06-26 21:27:53 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelb6c02712008-06-26 21:27:53 +02004 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010020#include <linux/ratelimit.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020021#include <linux/pci.h>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020022#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080023#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010025#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020026#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090027#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020028#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010029#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020030#include <linux/delay.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020031#include <linux/amd-iommu.h>
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010032#include <linux/notifier.h>
33#include <linux/export.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020034#include <linux/irq.h>
35#include <linux/msi.h>
Joerg Roedel3b839a52015-04-01 14:58:47 +020036#include <linux/dma-contiguous.h>
Jiang Liu7c71d302015-04-13 14:11:33 +080037#include <linux/irqdomain.h>
Joerg Roedel5f6bed52015-12-22 13:34:22 +010038#include <linux/percpu.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020039#include <asm/irq_remapping.h>
40#include <asm/io_apic.h>
41#include <asm/apic.h>
42#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020043#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020044#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090045#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010046#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020047#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020048
49#include "amd_iommu_proto.h"
50#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020051#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020052
53#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
54
Joerg Roedel815b33f2011-04-06 17:26:49 +020055#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020056
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020057/*
58 * This bitmap is used to advertise the page sizes our hardware support
59 * to the IOMMU core, which will then use this information to split
60 * physically contiguous memory regions it is mapping into page sizes
61 * that we support.
62 *
Joerg Roedel954e3dd2012-12-02 15:35:37 +010063 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020064 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010065#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020066
Joerg Roedelb6c02712008-06-26 21:27:53 +020067static DEFINE_RWLOCK(amd_iommu_devtable_lock);
68
Joerg Roedel8fa5f802011-06-09 12:24:45 +020069/* List of all available dev_data structures */
70static LIST_HEAD(dev_data_list);
71static DEFINE_SPINLOCK(dev_data_list_lock);
72
Joerg Roedel6efed632012-06-14 15:52:58 +020073LIST_HEAD(ioapic_map);
74LIST_HEAD(hpet_map);
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040075LIST_HEAD(acpihid_map);
Joerg Roedel6efed632012-06-14 15:52:58 +020076
Joerg Roedel0feae532009-08-26 15:26:30 +020077/*
78 * Domain for untranslated devices - only allocated
79 * if iommu=pt passed on kernel cmd line.
80 */
Thierry Redingb22f6432014-06-27 09:03:12 +020081static const struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010082
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010083static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010084int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010085
Joerg Roedelac1534a2012-06-21 14:52:40 +020086static struct dma_map_ops amd_iommu_dma_ops;
87
Joerg Roedel431b2a22008-07-11 17:14:22 +020088/*
Joerg Roedel50917e22014-08-05 16:38:38 +020089 * This struct contains device specific data for the IOMMU
90 */
91struct iommu_dev_data {
92 struct list_head list; /* For domain->dev_list */
93 struct list_head dev_data_list; /* For global dev_data_list */
Joerg Roedel50917e22014-08-05 16:38:38 +020094 struct protection_domain *domain; /* Domain the device is bound to */
Joerg Roedel50917e22014-08-05 16:38:38 +020095 u16 devid; /* PCI Device ID */
96 bool iommu_v2; /* Device can make use of IOMMUv2 */
Joerg Roedel1e6a7b02015-07-28 16:58:48 +020097 bool passthrough; /* Device is identity mapped */
Joerg Roedel50917e22014-08-05 16:38:38 +020098 struct {
99 bool enabled;
100 int qdep;
101 } ats; /* ATS state */
102 bool pri_tlp; /* PASID TLB required for
103 PPR completions */
104 u32 errata; /* Bitmap for errata to apply */
105};
106
107/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200108 * general struct to manage commands send to an IOMMU
109 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200110struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200111 u32 data[4];
112};
113
Joerg Roedel05152a02012-06-15 16:53:51 +0200114struct kmem_cache *amd_iommu_irq_cache;
115
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200116static void update_domain(struct protection_domain *domain);
Joerg Roedel7a5a5662015-06-30 08:56:11 +0200117static int protection_domain_init(struct protection_domain *domain);
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100118static void detach_device(struct device *dev);
Chris Wrightc1eee672009-05-21 00:56:58 -0700119
Joerg Roedel007b74b2015-12-21 12:53:54 +0100120/*
121 * For dynamic growth the aperture size is split into ranges of 128MB of
122 * DMA address space each. This struct represents one such range.
123 */
124struct aperture_range {
125
Joerg Roedel08c5fb92015-12-21 13:04:49 +0100126 spinlock_t bitmap_lock;
127
Joerg Roedel007b74b2015-12-21 12:53:54 +0100128 /* address allocation bitmap */
129 unsigned long *bitmap;
Joerg Roedelae62d492015-12-21 16:28:45 +0100130 unsigned long offset;
Joerg Roedel60e6a7c2015-12-21 16:53:17 +0100131 unsigned long next_bit;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100132
133 /*
134 * Array of PTE pages for the aperture. In this array we save all the
135 * leaf pages of the domain page table used for the aperture. This way
136 * we don't need to walk the page table to find a specific PTE. We can
137 * just calculate its address in constant time.
138 */
139 u64 *pte_pages[64];
Joerg Roedel007b74b2015-12-21 12:53:54 +0100140};
141
142/*
143 * Data container for a dma_ops specific protection domain
144 */
145struct dma_ops_domain {
146 /* generic protection domain information */
147 struct protection_domain domain;
148
149 /* size of the aperture for the mappings */
150 unsigned long aperture_size;
151
Joerg Roedelebaecb42015-12-21 18:11:32 +0100152 /* aperture index we start searching for free addresses */
Joerg Roedel5f6bed52015-12-22 13:34:22 +0100153 u32 __percpu *next_index;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100154
155 /* address space relevant data */
156 struct aperture_range *aperture[APERTURE_MAX_RANGES];
Joerg Roedel007b74b2015-12-21 12:53:54 +0100157};
158
Joerg Roedel15898bb2009-11-24 15:39:42 +0100159/****************************************************************************
160 *
161 * Helper functions
162 *
163 ****************************************************************************/
164
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100165static struct protection_domain *to_pdomain(struct iommu_domain *dom)
166{
167 return container_of(dom, struct protection_domain, domain);
168}
169
Joerg Roedelf62dda62011-06-09 12:55:35 +0200170static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200171{
172 struct iommu_dev_data *dev_data;
173 unsigned long flags;
174
175 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
176 if (!dev_data)
177 return NULL;
178
Joerg Roedelf62dda62011-06-09 12:55:35 +0200179 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200180
181 spin_lock_irqsave(&dev_data_list_lock, flags);
182 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
183 spin_unlock_irqrestore(&dev_data_list_lock, flags);
184
185 return dev_data;
186}
187
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200188static struct iommu_dev_data *search_dev_data(u16 devid)
189{
190 struct iommu_dev_data *dev_data;
191 unsigned long flags;
192
193 spin_lock_irqsave(&dev_data_list_lock, flags);
194 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
195 if (dev_data->devid == devid)
196 goto out_unlock;
197 }
198
199 dev_data = NULL;
200
201out_unlock:
202 spin_unlock_irqrestore(&dev_data_list_lock, flags);
203
204 return dev_data;
205}
206
207static struct iommu_dev_data *find_dev_data(u16 devid)
208{
209 struct iommu_dev_data *dev_data;
210
211 dev_data = search_dev_data(devid);
212
213 if (dev_data == NULL)
214 dev_data = alloc_dev_data(devid);
215
216 return dev_data;
217}
218
Joerg Roedel15898bb2009-11-24 15:39:42 +0100219static inline u16 get_device_id(struct device *dev)
220{
221 struct pci_dev *pdev = to_pci_dev(dev);
222
Shuah Khan6f2729b2013-02-27 17:07:30 -0700223 return PCI_DEVID(pdev->bus->number, pdev->devfn);
Joerg Roedel15898bb2009-11-24 15:39:42 +0100224}
225
Joerg Roedel657cbb62009-11-23 15:26:46 +0100226static struct iommu_dev_data *get_dev_data(struct device *dev)
227{
228 return dev->archdata.iommu;
229}
230
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100231static bool pci_iommuv2_capable(struct pci_dev *pdev)
232{
233 static const int caps[] = {
234 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100235 PCI_EXT_CAP_ID_PRI,
236 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100237 };
238 int i, pos;
239
240 for (i = 0; i < 3; ++i) {
241 pos = pci_find_ext_capability(pdev, caps[i]);
242 if (pos == 0)
243 return false;
244 }
245
246 return true;
247}
248
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100249static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
250{
251 struct iommu_dev_data *dev_data;
252
253 dev_data = get_dev_data(&pdev->dev);
254
255 return dev_data->errata & (1 << erratum) ? true : false;
256}
257
Joerg Roedel71c70982009-11-24 16:43:06 +0100258/*
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200259 * This function actually applies the mapping to the page table of the
260 * dma_ops domain.
Joerg Roedel71c70982009-11-24 16:43:06 +0100261 */
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200262static void alloc_unity_mapping(struct dma_ops_domain *dma_dom,
263 struct unity_map_entry *e)
Joerg Roedel71c70982009-11-24 16:43:06 +0100264{
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200265 u64 addr;
Joerg Roedel71c70982009-11-24 16:43:06 +0100266
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200267 for (addr = e->address_start; addr < e->address_end;
268 addr += PAGE_SIZE) {
269 if (addr < dma_dom->aperture_size)
270 __set_bit(addr >> PAGE_SHIFT,
271 dma_dom->aperture[0]->bitmap);
Joerg Roedel71c70982009-11-24 16:43:06 +0100272 }
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200273}
Joerg Roedel71c70982009-11-24 16:43:06 +0100274
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200275/*
276 * Inits the unity mappings required for a specific device
277 */
278static void init_unity_mappings_for_device(struct device *dev,
279 struct dma_ops_domain *dma_dom)
280{
281 struct unity_map_entry *e;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400282 int devid;
Joerg Roedel71c70982009-11-24 16:43:06 +0100283
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200284 devid = get_device_id(dev);
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400285 if (IS_ERR_VALUE(devid))
286 return;
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200287
288 list_for_each_entry(e, &amd_iommu_unity_map, list) {
289 if (!(devid >= e->devid_start && devid <= e->devid_end))
290 continue;
291 alloc_unity_mapping(dma_dom, e);
292 }
Joerg Roedel71c70982009-11-24 16:43:06 +0100293}
294
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100295/*
296 * This function checks if the driver got a valid device from the caller to
297 * avoid dereferencing invalid pointers.
298 */
299static bool check_device(struct device *dev)
300{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400301 int devid;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100302
303 if (!dev || !dev->dma_mask)
304 return false;
305
Yijing Wangb82a2272013-12-05 19:42:41 +0800306 /* No PCI device */
307 if (!dev_is_pci(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100308 return false;
309
310 devid = get_device_id(dev);
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400311 if (IS_ERR_VALUE(devid))
312 return false;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100313
314 /* Out of our scope? */
315 if (devid > amd_iommu_last_bdf)
316 return false;
317
318 if (amd_iommu_rlookup_table[devid] == NULL)
319 return false;
320
321 return true;
322}
323
Alex Williamson25b11ce2014-09-19 10:03:13 -0600324static void init_iommu_group(struct device *dev)
Alex Williamson2851db22012-10-08 22:49:41 -0600325{
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200326 struct dma_ops_domain *dma_domain;
327 struct iommu_domain *domain;
Alex Williamson2851db22012-10-08 22:49:41 -0600328 struct iommu_group *group;
Alex Williamson2851db22012-10-08 22:49:41 -0600329
Alex Williamson65d53522014-07-03 09:51:30 -0600330 group = iommu_group_get_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200331 if (IS_ERR(group))
332 return;
333
334 domain = iommu_group_default_domain(group);
335 if (!domain)
336 goto out;
337
338 dma_domain = to_pdomain(domain)->priv;
339
340 init_unity_mappings_for_device(dev, dma_domain);
341out:
342 iommu_group_put(group);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600343}
344
345static int iommu_init_device(struct device *dev)
346{
347 struct pci_dev *pdev = to_pci_dev(dev);
348 struct iommu_dev_data *dev_data;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400349 int devid;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600350
351 if (dev->archdata.iommu)
352 return 0;
353
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400354 devid = get_device_id(dev);
355 if (IS_ERR_VALUE(devid))
356 return devid;
357
358 dev_data = find_dev_data(devid);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600359 if (!dev_data)
360 return -ENOMEM;
361
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100362 if (pci_iommuv2_capable(pdev)) {
363 struct amd_iommu *iommu;
364
365 iommu = amd_iommu_rlookup_table[dev_data->devid];
366 dev_data->iommu_v2 = iommu->is_iommu_v2;
367 }
368
Joerg Roedel657cbb62009-11-23 15:26:46 +0100369 dev->archdata.iommu = dev_data;
370
Alex Williamson066f2e92014-06-12 16:12:37 -0600371 iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
372 dev);
373
Joerg Roedel657cbb62009-11-23 15:26:46 +0100374 return 0;
375}
376
Joerg Roedel26018872011-06-06 16:50:14 +0200377static void iommu_ignore_device(struct device *dev)
378{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400379 u16 alias;
380 int devid;
Joerg Roedel26018872011-06-06 16:50:14 +0200381
382 devid = get_device_id(dev);
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400383 if (IS_ERR_VALUE(devid))
384 return;
385
Joerg Roedel26018872011-06-06 16:50:14 +0200386 alias = amd_iommu_alias_table[devid];
387
388 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
389 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
390
391 amd_iommu_rlookup_table[devid] = NULL;
392 amd_iommu_rlookup_table[alias] = NULL;
393}
394
Joerg Roedel657cbb62009-11-23 15:26:46 +0100395static void iommu_uninit_device(struct device *dev)
396{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400397 int devid;
398 struct iommu_dev_data *dev_data;
Alex Williamsonc1931092014-07-03 09:51:24 -0600399
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400400 devid = get_device_id(dev);
401 if (IS_ERR_VALUE(devid))
402 return;
403
404 dev_data = search_dev_data(devid);
Alex Williamsonc1931092014-07-03 09:51:24 -0600405 if (!dev_data)
406 return;
407
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100408 if (dev_data->domain)
409 detach_device(dev);
410
Alex Williamson066f2e92014-06-12 16:12:37 -0600411 iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
412 dev);
413
Alex Williamson9dcd6132012-05-30 14:19:07 -0600414 iommu_group_remove_device(dev);
415
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200416 /* Remove dma-ops */
417 dev->archdata.dma_ops = NULL;
418
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200419 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600420 * We keep dev_data around for unplugged devices and reuse it when the
421 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200422 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100423}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100424
Joerg Roedel7f265082008-12-12 13:50:21 +0100425#ifdef CONFIG_AMD_IOMMU_STATS
426
427/*
428 * Initialization code for statistics collection
429 */
430
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100431DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100432DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100433DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100434DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100435DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100436DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100437DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100438DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100439DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100440DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100441DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100442DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100443DECLARE_STATS_COUNTER(complete_ppr);
444DECLARE_STATS_COUNTER(invalidate_iotlb);
445DECLARE_STATS_COUNTER(invalidate_iotlb_all);
446DECLARE_STATS_COUNTER(pri_requests);
447
Joerg Roedel7f265082008-12-12 13:50:21 +0100448static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100449static struct dentry *de_fflush;
450
451static void amd_iommu_stats_add(struct __iommu_counter *cnt)
452{
453 if (stats_dir == NULL)
454 return;
455
456 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
457 &cnt->value);
458}
459
460static void amd_iommu_stats_init(void)
461{
462 stats_dir = debugfs_create_dir("amd-iommu", NULL);
463 if (stats_dir == NULL)
464 return;
465
Joerg Roedel7f265082008-12-12 13:50:21 +0100466 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300467 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100468
469 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100470 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100471 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100472 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100473 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100474 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100475 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100476 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100477 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100478 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100479 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100480 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100481 amd_iommu_stats_add(&complete_ppr);
482 amd_iommu_stats_add(&invalidate_iotlb);
483 amd_iommu_stats_add(&invalidate_iotlb_all);
484 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100485}
486
487#endif
488
Joerg Roedel431b2a22008-07-11 17:14:22 +0200489/****************************************************************************
490 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200491 * Interrupt handling functions
492 *
493 ****************************************************************************/
494
Joerg Roedele3e59872009-09-03 14:02:10 +0200495static void dump_dte_entry(u16 devid)
496{
497 int i;
498
Joerg Roedelee6c2862011-11-09 12:06:03 +0100499 for (i = 0; i < 4; ++i)
500 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200501 amd_iommu_dev_table[devid].data[i]);
502}
503
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200504static void dump_command(unsigned long phys_addr)
505{
506 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
507 int i;
508
509 for (i = 0; i < 4; ++i)
510 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
511}
512
Joerg Roedela345b232009-09-03 15:01:43 +0200513static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200514{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200515 int type, devid, domid, flags;
516 volatile u32 *event = __evt;
517 int count = 0;
518 u64 address;
519
520retry:
521 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
522 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
523 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
524 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
525 address = (u64)(((u64)event[3]) << 32) | event[2];
526
527 if (type == 0) {
528 /* Did we hit the erratum? */
529 if (++count == LOOP_TIMEOUT) {
530 pr_err("AMD-Vi: No event written to event log\n");
531 return;
532 }
533 udelay(1);
534 goto retry;
535 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200536
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200537 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200538
539 switch (type) {
540 case EVENT_TYPE_ILL_DEV:
541 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
542 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700543 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200544 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200545 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200546 break;
547 case EVENT_TYPE_IO_FAULT:
548 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
549 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700550 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200551 domid, address, flags);
552 break;
553 case EVENT_TYPE_DEV_TAB_ERR:
554 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
555 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700556 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200557 address, flags);
558 break;
559 case EVENT_TYPE_PAGE_TAB_ERR:
560 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
561 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700562 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200563 domid, address, flags);
564 break;
565 case EVENT_TYPE_ILL_CMD:
566 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200567 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200568 break;
569 case EVENT_TYPE_CMD_HARD_ERR:
570 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
571 "flags=0x%04x]\n", address, flags);
572 break;
573 case EVENT_TYPE_IOTLB_INV_TO:
574 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
575 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700576 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200577 address);
578 break;
579 case EVENT_TYPE_INV_DEV_REQ:
580 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
581 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700582 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200583 address, flags);
584 break;
585 default:
586 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
587 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200588
589 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200590}
591
592static void iommu_poll_events(struct amd_iommu *iommu)
593{
594 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200595
596 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
597 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
598
599 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200600 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200601 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200602 }
603
604 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200605}
606
Joerg Roedeleee53532012-06-01 15:20:23 +0200607static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100608{
609 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100610
Joerg Roedel399be2f2011-12-01 16:53:47 +0100611 INC_STATS_COUNTER(pri_requests);
612
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100613 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
614 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
615 return;
616 }
617
618 fault.address = raw[1];
619 fault.pasid = PPR_PASID(raw[0]);
620 fault.device_id = PPR_DEVID(raw[0]);
621 fault.tag = PPR_TAG(raw[0]);
622 fault.flags = PPR_FLAGS(raw[0]);
623
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100624 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
625}
626
627static void iommu_poll_ppr_log(struct amd_iommu *iommu)
628{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100629 u32 head, tail;
630
631 if (iommu->ppr_log == NULL)
632 return;
633
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100634 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
635 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
636
637 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200638 volatile u64 *raw;
639 u64 entry[2];
640 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100641
Joerg Roedeleee53532012-06-01 15:20:23 +0200642 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100643
Joerg Roedeleee53532012-06-01 15:20:23 +0200644 /*
645 * Hardware bug: Interrupt may arrive before the entry is
646 * written to memory. If this happens we need to wait for the
647 * entry to arrive.
648 */
649 for (i = 0; i < LOOP_TIMEOUT; ++i) {
650 if (PPR_REQ_TYPE(raw[0]) != 0)
651 break;
652 udelay(1);
653 }
654
655 /* Avoid memcpy function-call overhead */
656 entry[0] = raw[0];
657 entry[1] = raw[1];
658
659 /*
660 * To detect the hardware bug we need to clear the entry
661 * back to zero.
662 */
663 raw[0] = raw[1] = 0UL;
664
665 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100666 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
667 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200668
Joerg Roedeleee53532012-06-01 15:20:23 +0200669 /* Handle PPR entry */
670 iommu_handle_ppr_entry(iommu, entry);
671
Joerg Roedeleee53532012-06-01 15:20:23 +0200672 /* Refresh ring-buffer information */
673 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100674 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
675 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100676}
677
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200678irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200679{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500680 struct amd_iommu *iommu = (struct amd_iommu *) data;
681 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200682
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500683 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
684 /* Enable EVT and PPR interrupts again */
685 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
686 iommu->mmio_base + MMIO_STATUS_OFFSET);
687
688 if (status & MMIO_STATUS_EVT_INT_MASK) {
689 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
690 iommu_poll_events(iommu);
691 }
692
693 if (status & MMIO_STATUS_PPR_INT_MASK) {
694 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
695 iommu_poll_ppr_log(iommu);
696 }
697
698 /*
699 * Hardware bug: ERBT1312
700 * When re-enabling interrupt (by writing 1
701 * to clear the bit), the hardware might also try to set
702 * the interrupt bit in the event status register.
703 * In this scenario, the bit will be set, and disable
704 * subsequent interrupts.
705 *
706 * Workaround: The IOMMU driver should read back the
707 * status register and check if the interrupt bits are cleared.
708 * If not, driver will need to go through the interrupt handler
709 * again and re-clear the bits
710 */
711 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100712 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200713 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200714}
715
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200716irqreturn_t amd_iommu_int_handler(int irq, void *data)
717{
718 return IRQ_WAKE_THREAD;
719}
720
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200721/****************************************************************************
722 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200723 * IOMMU command queuing functions
724 *
725 ****************************************************************************/
726
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200727static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200728{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200729 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200730
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200731 while (*sem == 0 && i < LOOP_TIMEOUT) {
732 udelay(1);
733 i += 1;
734 }
735
736 if (i == LOOP_TIMEOUT) {
737 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
738 return -EIO;
739 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200740
741 return 0;
742}
743
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200744static void copy_cmd_to_buffer(struct amd_iommu *iommu,
745 struct iommu_cmd *cmd,
746 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200747{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200748 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200749
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200750 target = iommu->cmd_buf + tail;
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200751 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200752
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200753 /* Copy command to buffer */
754 memcpy(target, cmd, sizeof(*cmd));
755
756 /* Tell the IOMMU about it */
757 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
758}
759
Joerg Roedel815b33f2011-04-06 17:26:49 +0200760static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200761{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200762 WARN_ON(address & 0x7ULL);
763
Joerg Roedelded46732011-04-06 10:53:48 +0200764 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200765 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
766 cmd->data[1] = upper_32_bits(__pa(address));
767 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200768 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
769}
770
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200771static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
772{
773 memset(cmd, 0, sizeof(*cmd));
774 cmd->data[0] = devid;
775 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
776}
777
Joerg Roedel11b64022011-04-06 11:49:28 +0200778static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
779 size_t size, u16 domid, int pde)
780{
781 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100782 bool s;
Joerg Roedel11b64022011-04-06 11:49:28 +0200783
784 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100785 s = false;
Joerg Roedel11b64022011-04-06 11:49:28 +0200786
787 if (pages > 1) {
788 /*
789 * If we have to flush more than one page, flush all
790 * TLB entries for this domain
791 */
792 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100793 s = true;
Joerg Roedel11b64022011-04-06 11:49:28 +0200794 }
795
796 address &= PAGE_MASK;
797
798 memset(cmd, 0, sizeof(*cmd));
799 cmd->data[1] |= domid;
800 cmd->data[2] = lower_32_bits(address);
801 cmd->data[3] = upper_32_bits(address);
802 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
803 if (s) /* size bit - we flush more than one 4kb page */
804 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200805 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200806 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
807}
808
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200809static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
810 u64 address, size_t size)
811{
812 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100813 bool s;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200814
815 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100816 s = false;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200817
818 if (pages > 1) {
819 /*
820 * If we have to flush more than one page, flush all
821 * TLB entries for this domain
822 */
823 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100824 s = true;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200825 }
826
827 address &= PAGE_MASK;
828
829 memset(cmd, 0, sizeof(*cmd));
830 cmd->data[0] = devid;
831 cmd->data[0] |= (qdep & 0xff) << 24;
832 cmd->data[1] = devid;
833 cmd->data[2] = lower_32_bits(address);
834 cmd->data[3] = upper_32_bits(address);
835 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
836 if (s)
837 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
838}
839
Joerg Roedel22e266c2011-11-21 15:59:08 +0100840static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
841 u64 address, bool size)
842{
843 memset(cmd, 0, sizeof(*cmd));
844
845 address &= ~(0xfffULL);
846
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600847 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100848 cmd->data[1] = domid;
849 cmd->data[2] = lower_32_bits(address);
850 cmd->data[3] = upper_32_bits(address);
851 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
852 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
853 if (size)
854 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
855 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
856}
857
858static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
859 int qdep, u64 address, bool size)
860{
861 memset(cmd, 0, sizeof(*cmd));
862
863 address &= ~(0xfffULL);
864
865 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600866 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100867 cmd->data[0] |= (qdep & 0xff) << 24;
868 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600869 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100870 cmd->data[2] = lower_32_bits(address);
871 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
872 cmd->data[3] = upper_32_bits(address);
873 if (size)
874 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
875 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
876}
877
Joerg Roedelc99afa22011-11-21 18:19:25 +0100878static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
879 int status, int tag, bool gn)
880{
881 memset(cmd, 0, sizeof(*cmd));
882
883 cmd->data[0] = devid;
884 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600885 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +0100886 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
887 }
888 cmd->data[3] = tag & 0x1ff;
889 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
890
891 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
892}
893
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200894static void build_inv_all(struct iommu_cmd *cmd)
895{
896 memset(cmd, 0, sizeof(*cmd));
897 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200898}
899
Joerg Roedel7ef27982012-06-21 16:46:04 +0200900static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
901{
902 memset(cmd, 0, sizeof(*cmd));
903 cmd->data[0] = devid;
904 CMD_SET_TYPE(cmd, CMD_INV_IRT);
905}
906
Joerg Roedel431b2a22008-07-11 17:14:22 +0200907/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200908 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200909 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200910 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200911static int iommu_queue_command_sync(struct amd_iommu *iommu,
912 struct iommu_cmd *cmd,
913 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200914{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200915 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200916 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200917
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200918again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200919 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200920
921 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
922 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200923 next_tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
924 left = (head - next_tail) % CMD_BUFFER_SIZE;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200925
926 if (left <= 2) {
927 struct iommu_cmd sync_cmd;
928 volatile u64 sem = 0;
929 int ret;
930
931 build_completion_wait(&sync_cmd, (u64)&sem);
932 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
933
934 spin_unlock_irqrestore(&iommu->lock, flags);
935
936 if ((ret = wait_on_sem(&sem)) != 0)
937 return ret;
938
939 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200940 }
941
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200942 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200943
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200944 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200945 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200946
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200947 spin_unlock_irqrestore(&iommu->lock, flags);
948
Joerg Roedel815b33f2011-04-06 17:26:49 +0200949 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100950}
951
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200952static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
953{
954 return iommu_queue_command_sync(iommu, cmd, true);
955}
956
Joerg Roedel8d201962008-12-02 20:34:41 +0100957/*
958 * This function queues a completion wait command into the command
959 * buffer of an IOMMU
960 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100961static int iommu_completion_wait(struct amd_iommu *iommu)
962{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200963 struct iommu_cmd cmd;
964 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200965 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100966
967 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200968 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100969
Joerg Roedel815b33f2011-04-06 17:26:49 +0200970 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100971
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200972 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100973 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200974 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100975
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200976 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200977}
978
Joerg Roedeld8c13082011-04-06 18:51:26 +0200979static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200980{
981 struct iommu_cmd cmd;
982
Joerg Roedeld8c13082011-04-06 18:51:26 +0200983 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200984
Joerg Roedeld8c13082011-04-06 18:51:26 +0200985 return iommu_queue_command(iommu, &cmd);
986}
987
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200988static void iommu_flush_dte_all(struct amd_iommu *iommu)
989{
990 u32 devid;
991
992 for (devid = 0; devid <= 0xffff; ++devid)
993 iommu_flush_dte(iommu, devid);
994
995 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200996}
997
998/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200999 * This function uses heavy locking and may disable irqs for some time. But
1000 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001001 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001002static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001003{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001004 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001005
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001006 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1007 struct iommu_cmd cmd;
1008 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1009 dom_id, 1);
1010 iommu_queue_command(iommu, &cmd);
1011 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001012
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001013 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001014}
1015
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001016static void iommu_flush_all(struct amd_iommu *iommu)
1017{
1018 struct iommu_cmd cmd;
1019
1020 build_inv_all(&cmd);
1021
1022 iommu_queue_command(iommu, &cmd);
1023 iommu_completion_wait(iommu);
1024}
1025
Joerg Roedel7ef27982012-06-21 16:46:04 +02001026static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1027{
1028 struct iommu_cmd cmd;
1029
1030 build_inv_irt(&cmd, devid);
1031
1032 iommu_queue_command(iommu, &cmd);
1033}
1034
1035static void iommu_flush_irt_all(struct amd_iommu *iommu)
1036{
1037 u32 devid;
1038
1039 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1040 iommu_flush_irt(iommu, devid);
1041
1042 iommu_completion_wait(iommu);
1043}
1044
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001045void iommu_flush_all_caches(struct amd_iommu *iommu)
1046{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001047 if (iommu_feature(iommu, FEATURE_IA)) {
1048 iommu_flush_all(iommu);
1049 } else {
1050 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001051 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001052 iommu_flush_tlb_all(iommu);
1053 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001054}
1055
Joerg Roedel431b2a22008-07-11 17:14:22 +02001056/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001057 * Command send function for flushing on-device TLB
1058 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001059static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1060 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001061{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001062 struct amd_iommu *iommu;
1063 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001064 int qdep;
1065
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001066 qdep = dev_data->ats.qdep;
1067 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001068
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001069 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001070
1071 return iommu_queue_command(iommu, &cmd);
1072}
1073
1074/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001075 * Command send function for invalidating a device table entry
1076 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001077static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001078{
1079 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001080 u16 alias;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001081 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001082
Joerg Roedel6c542042011-06-09 17:07:31 +02001083 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02001084 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001085
Joerg Roedelf62dda62011-06-09 12:55:35 +02001086 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001087 if (!ret && alias != dev_data->devid)
1088 ret = iommu_flush_dte(iommu, alias);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001089 if (ret)
1090 return ret;
1091
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001092 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001093 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001094
1095 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001096}
1097
Joerg Roedel431b2a22008-07-11 17:14:22 +02001098/*
1099 * TLB invalidation function which is called from the mapping functions.
1100 * It invalidates a single PTE if the range to flush is within a single
1101 * page. Otherwise it flushes the whole TLB of the IOMMU.
1102 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001103static void __domain_flush_pages(struct protection_domain *domain,
1104 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001105{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001106 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001107 struct iommu_cmd cmd;
1108 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001109
Joerg Roedel11b64022011-04-06 11:49:28 +02001110 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001111
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001112 for (i = 0; i < amd_iommus_present; ++i) {
1113 if (!domain->dev_iommu[i])
1114 continue;
1115
1116 /*
1117 * Devices of this domain are behind this IOMMU
1118 * We need a TLB flush
1119 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001120 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001121 }
1122
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001123 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001124
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001125 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001126 continue;
1127
Joerg Roedel6c542042011-06-09 17:07:31 +02001128 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001129 }
1130
Joerg Roedel11b64022011-04-06 11:49:28 +02001131 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001132}
1133
Joerg Roedel17b124b2011-04-06 18:01:35 +02001134static void domain_flush_pages(struct protection_domain *domain,
1135 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001136{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001137 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001138}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001139
Joerg Roedel1c655772008-09-04 18:40:05 +02001140/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001141static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001142{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001143 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001144}
1145
Chris Wright42a49f92009-06-15 15:42:00 +02001146/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001147static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001148{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001149 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1150}
1151
1152static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001153{
1154 int i;
1155
1156 for (i = 0; i < amd_iommus_present; ++i) {
1157 if (!domain->dev_iommu[i])
1158 continue;
1159
1160 /*
1161 * Devices of this domain are behind this IOMMU
1162 * We need to wait for completion of all commands.
1163 */
1164 iommu_completion_wait(amd_iommus[i]);
1165 }
1166}
1167
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001168
Joerg Roedel43f49602008-12-02 21:01:12 +01001169/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001170 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001171 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001172static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001173{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001174 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001175
1176 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001177 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001178}
1179
Joerg Roedel431b2a22008-07-11 17:14:22 +02001180/****************************************************************************
1181 *
1182 * The functions below are used the create the page table mappings for
1183 * unity mapped regions.
1184 *
1185 ****************************************************************************/
1186
1187/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001188 * This function is used to add another level to an IO page table. Adding
1189 * another level increases the size of the address space by 9 bits to a size up
1190 * to 64 bits.
1191 */
1192static bool increase_address_space(struct protection_domain *domain,
1193 gfp_t gfp)
1194{
1195 u64 *pte;
1196
1197 if (domain->mode == PAGE_MODE_6_LEVEL)
1198 /* address space already 64 bit large */
1199 return false;
1200
1201 pte = (void *)get_zeroed_page(gfp);
1202 if (!pte)
1203 return false;
1204
1205 *pte = PM_LEVEL_PDE(domain->mode,
1206 virt_to_phys(domain->pt_root));
1207 domain->pt_root = pte;
1208 domain->mode += 1;
1209 domain->updated = true;
1210
1211 return true;
1212}
1213
1214static u64 *alloc_pte(struct protection_domain *domain,
1215 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001216 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001217 u64 **pte_page,
1218 gfp_t gfp)
1219{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001220 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001221 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001222
1223 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001224
1225 while (address > PM_LEVEL_SIZE(domain->mode))
1226 increase_address_space(domain, gfp);
1227
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001228 level = domain->mode - 1;
1229 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1230 address = PAGE_SIZE_ALIGN(address, page_size);
1231 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001232
1233 while (level > end_lvl) {
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001234 u64 __pte, __npte;
1235
1236 __pte = *pte;
1237
1238 if (!IOMMU_PTE_PRESENT(__pte)) {
Joerg Roedel308973d2009-11-24 17:43:32 +01001239 page = (u64 *)get_zeroed_page(gfp);
1240 if (!page)
1241 return NULL;
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001242
1243 __npte = PM_LEVEL_PDE(level, virt_to_phys(page));
1244
1245 if (cmpxchg64(pte, __pte, __npte)) {
1246 free_page((unsigned long)page);
1247 continue;
1248 }
Joerg Roedel308973d2009-11-24 17:43:32 +01001249 }
1250
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001251 /* No level skipping support yet */
1252 if (PM_PTE_LEVEL(*pte) != level)
1253 return NULL;
1254
Joerg Roedel308973d2009-11-24 17:43:32 +01001255 level -= 1;
1256
1257 pte = IOMMU_PTE_PAGE(*pte);
1258
1259 if (pte_page && level == end_lvl)
1260 *pte_page = pte;
1261
1262 pte = &pte[PM_LEVEL_INDEX(level, address)];
1263 }
1264
1265 return pte;
1266}
1267
1268/*
1269 * This function checks if there is a PTE for a given dma address. If
1270 * there is one, it returns the pointer to it.
1271 */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001272static u64 *fetch_pte(struct protection_domain *domain,
1273 unsigned long address,
1274 unsigned long *page_size)
Joerg Roedel308973d2009-11-24 17:43:32 +01001275{
1276 int level;
1277 u64 *pte;
1278
Joerg Roedel24cd7722010-01-19 17:27:39 +01001279 if (address > PM_LEVEL_SIZE(domain->mode))
1280 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001281
Joerg Roedel3039ca12015-04-01 14:58:48 +02001282 level = domain->mode - 1;
1283 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1284 *page_size = PTE_LEVEL_PAGE_SIZE(level);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001285
1286 while (level > 0) {
1287
1288 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001289 if (!IOMMU_PTE_PRESENT(*pte))
1290 return NULL;
1291
Joerg Roedel24cd7722010-01-19 17:27:39 +01001292 /* Large PTE */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001293 if (PM_PTE_LEVEL(*pte) == 7 ||
1294 PM_PTE_LEVEL(*pte) == 0)
1295 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001296
1297 /* No level skipping support yet */
1298 if (PM_PTE_LEVEL(*pte) != level)
1299 return NULL;
1300
Joerg Roedel308973d2009-11-24 17:43:32 +01001301 level -= 1;
1302
Joerg Roedel24cd7722010-01-19 17:27:39 +01001303 /* Walk to the next level */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001304 pte = IOMMU_PTE_PAGE(*pte);
1305 pte = &pte[PM_LEVEL_INDEX(level, address)];
1306 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1307 }
1308
1309 if (PM_PTE_LEVEL(*pte) == 0x07) {
1310 unsigned long pte_mask;
1311
1312 /*
1313 * If we have a series of large PTEs, make
1314 * sure to return a pointer to the first one.
1315 */
1316 *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
1317 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1318 pte = (u64 *)(((unsigned long)pte) & pte_mask);
Joerg Roedel308973d2009-11-24 17:43:32 +01001319 }
1320
1321 return pte;
1322}
1323
1324/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001325 * Generic mapping functions. It maps a physical address into a DMA
1326 * address space. It allocates the page table pages if necessary.
1327 * In the future it can be extended to a generic mapping function
1328 * supporting all features of AMD IOMMU page tables like level skipping
1329 * and full 64 bit address spaces.
1330 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001331static int iommu_map_page(struct protection_domain *dom,
1332 unsigned long bus_addr,
1333 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001334 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001335 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001336{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001337 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001338 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001339
Joerg Roedeld4b03662015-04-01 14:58:52 +02001340 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1341 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1342
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001343 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001344 return -EINVAL;
1345
Joerg Roedeld4b03662015-04-01 14:58:52 +02001346 count = PAGE_SIZE_PTE_COUNT(page_size);
1347 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001348
Maurizio Lombardi63eaa752014-09-11 12:28:03 +02001349 if (!pte)
1350 return -ENOMEM;
1351
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001352 for (i = 0; i < count; ++i)
1353 if (IOMMU_PTE_PRESENT(pte[i]))
1354 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001355
Joerg Roedeld4b03662015-04-01 14:58:52 +02001356 if (count > 1) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001357 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1358 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1359 } else
1360 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1361
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001362 if (prot & IOMMU_PROT_IR)
1363 __pte |= IOMMU_PTE_IR;
1364 if (prot & IOMMU_PROT_IW)
1365 __pte |= IOMMU_PTE_IW;
1366
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001367 for (i = 0; i < count; ++i)
1368 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001369
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001370 update_domain(dom);
1371
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001372 return 0;
1373}
1374
Joerg Roedel24cd7722010-01-19 17:27:39 +01001375static unsigned long iommu_unmap_page(struct protection_domain *dom,
1376 unsigned long bus_addr,
1377 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001378{
Joerg Roedel71b390e2015-04-01 14:58:49 +02001379 unsigned long long unmapped;
1380 unsigned long unmap_size;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001381 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001382
Joerg Roedel24cd7722010-01-19 17:27:39 +01001383 BUG_ON(!is_power_of_2(page_size));
1384
1385 unmapped = 0;
1386
1387 while (unmapped < page_size) {
1388
Joerg Roedel71b390e2015-04-01 14:58:49 +02001389 pte = fetch_pte(dom, bus_addr, &unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001390
Joerg Roedel71b390e2015-04-01 14:58:49 +02001391 if (pte) {
1392 int i, count;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001393
Joerg Roedel71b390e2015-04-01 14:58:49 +02001394 count = PAGE_SIZE_PTE_COUNT(unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001395 for (i = 0; i < count; i++)
1396 pte[i] = 0ULL;
1397 }
1398
1399 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1400 unmapped += unmap_size;
1401 }
1402
Alex Williamson60d0ca32013-06-21 14:33:19 -06001403 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001404
1405 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001406}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001407
Joerg Roedel431b2a22008-07-11 17:14:22 +02001408/****************************************************************************
1409 *
1410 * The next functions belong to the address allocator for the dma_ops
1411 * interface functions. They work like the allocators in the other IOMMU
1412 * drivers. Its basically a bitmap which marks the allocated pages in
1413 * the aperture. Maybe it could be enhanced in the future to a more
1414 * efficient allocator.
1415 *
1416 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001417
Joerg Roedel431b2a22008-07-11 17:14:22 +02001418/*
Joerg Roedel384de722009-05-15 12:30:05 +02001419 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001420 *
1421 * called with domain->lock held
1422 */
Joerg Roedel384de722009-05-15 12:30:05 +02001423
Joerg Roedel9cabe892009-05-18 16:38:55 +02001424/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001425 * Used to reserve address ranges in the aperture (e.g. for exclusion
1426 * ranges.
1427 */
1428static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1429 unsigned long start_page,
1430 unsigned int pages)
1431{
1432 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1433
1434 if (start_page + pages > last_page)
1435 pages = last_page - start_page;
1436
1437 for (i = start_page; i < start_page + pages; ++i) {
1438 int index = i / APERTURE_RANGE_PAGES;
1439 int page = i % APERTURE_RANGE_PAGES;
1440 __set_bit(page, dom->aperture[index]->bitmap);
1441 }
1442}
1443
1444/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001445 * This function is used to add a new aperture range to an existing
1446 * aperture in case of dma_ops domain allocation or address allocation
1447 * failure.
1448 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001449static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001450 bool populate, gfp_t gfp)
1451{
1452 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001453 unsigned long i, old_size, pte_pgsize;
Joerg Roedela73c1562015-12-21 19:25:56 +01001454 struct aperture_range *range;
1455 struct amd_iommu *iommu;
1456 unsigned long flags;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001457
Joerg Roedelf5e97052009-05-22 12:31:53 +02001458#ifdef CONFIG_IOMMU_STRESS
1459 populate = false;
1460#endif
1461
Joerg Roedel9cabe892009-05-18 16:38:55 +02001462 if (index >= APERTURE_MAX_RANGES)
1463 return -ENOMEM;
1464
Joerg Roedela73c1562015-12-21 19:25:56 +01001465 range = kzalloc(sizeof(struct aperture_range), gfp);
1466 if (!range)
Joerg Roedel9cabe892009-05-18 16:38:55 +02001467 return -ENOMEM;
1468
Joerg Roedela73c1562015-12-21 19:25:56 +01001469 range->bitmap = (void *)get_zeroed_page(gfp);
1470 if (!range->bitmap)
Joerg Roedel9cabe892009-05-18 16:38:55 +02001471 goto out_free;
1472
Joerg Roedela73c1562015-12-21 19:25:56 +01001473 range->offset = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001474
Joerg Roedela73c1562015-12-21 19:25:56 +01001475 spin_lock_init(&range->bitmap_lock);
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001476
Joerg Roedel9cabe892009-05-18 16:38:55 +02001477 if (populate) {
1478 unsigned long address = dma_dom->aperture_size;
1479 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1480 u64 *pte, *pte_page;
1481
1482 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001483 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001484 &pte_page, gfp);
1485 if (!pte)
1486 goto out_free;
1487
Joerg Roedela73c1562015-12-21 19:25:56 +01001488 range->pte_pages[i] = pte_page;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001489
1490 address += APERTURE_RANGE_SIZE / 64;
1491 }
1492 }
1493
Joerg Roedel92d420e2015-12-21 19:31:33 +01001494 spin_lock_irqsave(&dma_dom->domain.lock, flags);
1495
Joerg Roedela73c1562015-12-21 19:25:56 +01001496 /* First take the bitmap_lock and then publish the range */
Joerg Roedel92d420e2015-12-21 19:31:33 +01001497 spin_lock(&range->bitmap_lock);
Joerg Roedela73c1562015-12-21 19:25:56 +01001498
1499 old_size = dma_dom->aperture_size;
1500 dma_dom->aperture[index] = range;
1501 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001502
Joerg Roedel17f5b562011-07-06 17:14:44 +02001503 /* Reserve address range used for MSI messages */
1504 if (old_size < MSI_ADDR_BASE_LO &&
1505 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1506 unsigned long spage;
1507 int pages;
1508
1509 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1510 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1511
1512 dma_ops_reserve_addresses(dma_dom, spage, pages);
1513 }
1514
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001515 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001516 for_each_iommu(iommu) {
1517 if (iommu->exclusion_start &&
1518 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1519 && iommu->exclusion_start < dma_dom->aperture_size) {
1520 unsigned long startpage;
1521 int pages = iommu_num_pages(iommu->exclusion_start,
1522 iommu->exclusion_length,
1523 PAGE_SIZE);
1524 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1525 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1526 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001527 }
1528
1529 /*
1530 * Check for areas already mapped as present in the new aperture
1531 * range and mark those pages as reserved in the allocator. Such
1532 * mappings may already exist as a result of requested unity
1533 * mappings for devices.
1534 */
1535 for (i = dma_dom->aperture[index]->offset;
1536 i < dma_dom->aperture_size;
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001537 i += pte_pgsize) {
Joerg Roedel3039ca12015-04-01 14:58:48 +02001538 u64 *pte = fetch_pte(&dma_dom->domain, i, &pte_pgsize);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001539 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1540 continue;
1541
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001542 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT,
1543 pte_pgsize >> 12);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001544 }
1545
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001546 update_domain(&dma_dom->domain);
1547
Joerg Roedel92d420e2015-12-21 19:31:33 +01001548 spin_unlock(&range->bitmap_lock);
1549
1550 spin_unlock_irqrestore(&dma_dom->domain.lock, flags);
Joerg Roedela73c1562015-12-21 19:25:56 +01001551
Joerg Roedel9cabe892009-05-18 16:38:55 +02001552 return 0;
1553
1554out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001555 update_domain(&dma_dom->domain);
1556
Joerg Roedela73c1562015-12-21 19:25:56 +01001557 free_page((unsigned long)range->bitmap);
Joerg Roedel9cabe892009-05-18 16:38:55 +02001558
Joerg Roedela73c1562015-12-21 19:25:56 +01001559 kfree(range);
Joerg Roedel9cabe892009-05-18 16:38:55 +02001560
1561 return -ENOMEM;
1562}
1563
Joerg Roedelccb50e02015-12-21 17:49:34 +01001564static dma_addr_t dma_ops_aperture_alloc(struct dma_ops_domain *dom,
1565 struct aperture_range *range,
Joerg Roedela0f51442015-12-21 16:20:09 +01001566 unsigned long pages,
Joerg Roedela0f51442015-12-21 16:20:09 +01001567 unsigned long dma_mask,
1568 unsigned long boundary_size,
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001569 unsigned long align_mask,
1570 bool trylock)
Joerg Roedela0f51442015-12-21 16:20:09 +01001571{
1572 unsigned long offset, limit, flags;
1573 dma_addr_t address;
Joerg Roedelccb50e02015-12-21 17:49:34 +01001574 bool flush = false;
Joerg Roedela0f51442015-12-21 16:20:09 +01001575
1576 offset = range->offset >> PAGE_SHIFT;
1577 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1578 dma_mask >> PAGE_SHIFT);
1579
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001580 if (trylock) {
1581 if (!spin_trylock_irqsave(&range->bitmap_lock, flags))
1582 return -1;
1583 } else {
1584 spin_lock_irqsave(&range->bitmap_lock, flags);
1585 }
1586
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001587 address = iommu_area_alloc(range->bitmap, limit, range->next_bit,
1588 pages, offset, boundary_size, align_mask);
Joerg Roedelccb50e02015-12-21 17:49:34 +01001589 if (address == -1) {
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001590 /* Nothing found, retry one time */
1591 address = iommu_area_alloc(range->bitmap, limit,
1592 0, pages, offset, boundary_size,
1593 align_mask);
Joerg Roedelccb50e02015-12-21 17:49:34 +01001594 flush = true;
1595 }
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001596
1597 if (address != -1)
1598 range->next_bit = address + pages;
1599
Joerg Roedela0f51442015-12-21 16:20:09 +01001600 spin_unlock_irqrestore(&range->bitmap_lock, flags);
1601
Joerg Roedelccb50e02015-12-21 17:49:34 +01001602 if (flush) {
1603 domain_flush_tlb(&dom->domain);
1604 domain_flush_complete(&dom->domain);
1605 }
1606
Joerg Roedela0f51442015-12-21 16:20:09 +01001607 return address;
1608}
1609
Joerg Roedel384de722009-05-15 12:30:05 +02001610static unsigned long dma_ops_area_alloc(struct device *dev,
1611 struct dma_ops_domain *dom,
1612 unsigned int pages,
1613 unsigned long align_mask,
Joerg Roedel05ab49e2015-12-21 17:58:26 +01001614 u64 dma_mask)
Joerg Roedel384de722009-05-15 12:30:05 +02001615{
Joerg Roedelab7032b2015-12-21 18:47:11 +01001616 unsigned long boundary_size, mask;
Joerg Roedel384de722009-05-15 12:30:05 +02001617 unsigned long address = -1;
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001618 bool first = true;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001619 u32 start, i;
1620
1621 preempt_disable();
Joerg Roedel384de722009-05-15 12:30:05 +02001622
Joerg Roedele6aabee2015-05-27 09:26:09 +02001623 mask = dma_get_seg_boundary(dev);
1624
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001625again:
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001626 start = this_cpu_read(*dom->next_index);
1627
1628 /* Sanity check - is it really necessary? */
1629 if (unlikely(start > APERTURE_MAX_RANGES)) {
1630 start = 0;
1631 this_cpu_write(*dom->next_index, 0);
1632 }
1633
Joerg Roedele6aabee2015-05-27 09:26:09 +02001634 boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT :
1635 1UL << (BITS_PER_LONG - PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001636
Joerg Roedel2a874422015-12-21 18:34:47 +01001637 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1638 struct aperture_range *range;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001639 int index;
Joerg Roedelccb50e02015-12-21 17:49:34 +01001640
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001641 index = (start + i) % APERTURE_MAX_RANGES;
1642
1643 range = dom->aperture[index];
Joerg Roedel2a874422015-12-21 18:34:47 +01001644
1645 if (!range || range->offset >= dma_mask)
1646 continue;
Joerg Roedel384de722009-05-15 12:30:05 +02001647
Joerg Roedel2a874422015-12-21 18:34:47 +01001648 address = dma_ops_aperture_alloc(dom, range, pages,
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001649 dma_mask, boundary_size,
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001650 align_mask, first);
Joerg Roedel384de722009-05-15 12:30:05 +02001651 if (address != -1) {
Joerg Roedel2a874422015-12-21 18:34:47 +01001652 address = range->offset + (address << PAGE_SHIFT);
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001653 this_cpu_write(*dom->next_index, index);
Joerg Roedel384de722009-05-15 12:30:05 +02001654 break;
1655 }
Joerg Roedel384de722009-05-15 12:30:05 +02001656 }
1657
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001658 if (address == -1 && first) {
1659 first = false;
1660 goto again;
1661 }
1662
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001663 preempt_enable();
1664
Joerg Roedel384de722009-05-15 12:30:05 +02001665 return address;
1666}
1667
Joerg Roedeld3086442008-06-26 21:27:57 +02001668static unsigned long dma_ops_alloc_addresses(struct device *dev,
1669 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001670 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001671 unsigned long align_mask,
1672 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001673{
Joerg Roedel266a3bd2015-12-21 18:54:24 +01001674 unsigned long address = -1;
Joerg Roedeld3086442008-06-26 21:27:57 +02001675
Joerg Roedel266a3bd2015-12-21 18:54:24 +01001676 while (address == -1) {
1677 address = dma_ops_area_alloc(dev, dom, pages,
1678 align_mask, dma_mask);
1679
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001680 if (address == -1 && alloc_new_range(dom, false, GFP_ATOMIC))
Joerg Roedel266a3bd2015-12-21 18:54:24 +01001681 break;
1682 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001683
Joerg Roedel384de722009-05-15 12:30:05 +02001684 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001685 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001686
1687 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1688
1689 return address;
1690}
1691
Joerg Roedel431b2a22008-07-11 17:14:22 +02001692/*
1693 * The address free function.
1694 *
1695 * called with domain->lock held
1696 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001697static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1698 unsigned long address,
1699 unsigned int pages)
1700{
Joerg Roedel384de722009-05-15 12:30:05 +02001701 unsigned i = address >> APERTURE_RANGE_SHIFT;
1702 struct aperture_range *range = dom->aperture[i];
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001703 unsigned long flags;
Joerg Roedel80be3082008-11-06 14:59:05 +01001704
Joerg Roedel384de722009-05-15 12:30:05 +02001705 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1706
Joerg Roedel47bccd62009-05-22 12:40:54 +02001707#ifdef CONFIG_IOMMU_STRESS
1708 if (i < 4)
1709 return;
1710#endif
1711
Joerg Roedel4eeca8c2015-12-22 12:15:35 +01001712 if (amd_iommu_unmap_flush) {
Joerg Roedeld41ab092015-12-21 18:20:03 +01001713 domain_flush_tlb(&dom->domain);
1714 domain_flush_complete(&dom->domain);
1715 }
Joerg Roedel384de722009-05-15 12:30:05 +02001716
1717 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001718
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001719 spin_lock_irqsave(&range->bitmap_lock, flags);
Joerg Roedel4eeca8c2015-12-22 12:15:35 +01001720 if (address + pages > range->next_bit)
1721 range->next_bit = address + pages;
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001722 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001723 spin_unlock_irqrestore(&range->bitmap_lock, flags);
Joerg Roedel384de722009-05-15 12:30:05 +02001724
Joerg Roedeld3086442008-06-26 21:27:57 +02001725}
1726
Joerg Roedel431b2a22008-07-11 17:14:22 +02001727/****************************************************************************
1728 *
1729 * The next functions belong to the domain allocation. A domain is
1730 * allocated for every IOMMU as the default domain. If device isolation
1731 * is enabled, every device get its own domain. The most important thing
1732 * about domains is the page table mapping the DMA address space they
1733 * contain.
1734 *
1735 ****************************************************************************/
1736
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001737/*
1738 * This function adds a protection domain to the global protection domain list
1739 */
1740static void add_domain_to_list(struct protection_domain *domain)
1741{
1742 unsigned long flags;
1743
1744 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1745 list_add(&domain->list, &amd_iommu_pd_list);
1746 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1747}
1748
1749/*
1750 * This function removes a protection domain to the global
1751 * protection domain list
1752 */
1753static void del_domain_from_list(struct protection_domain *domain)
1754{
1755 unsigned long flags;
1756
1757 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1758 list_del(&domain->list);
1759 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1760}
1761
Joerg Roedelec487d12008-06-26 21:27:58 +02001762static u16 domain_id_alloc(void)
1763{
1764 unsigned long flags;
1765 int id;
1766
1767 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1768 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1769 BUG_ON(id == 0);
1770 if (id > 0 && id < MAX_DOMAIN_ID)
1771 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1772 else
1773 id = 0;
1774 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1775
1776 return id;
1777}
1778
Joerg Roedela2acfb72008-12-02 18:28:53 +01001779static void domain_id_free(int id)
1780{
1781 unsigned long flags;
1782
1783 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1784 if (id > 0 && id < MAX_DOMAIN_ID)
1785 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1786 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1787}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001788
Joerg Roedel5c34c402013-06-20 20:22:58 +02001789#define DEFINE_FREE_PT_FN(LVL, FN) \
1790static void free_pt_##LVL (unsigned long __pt) \
1791{ \
1792 unsigned long p; \
1793 u64 *pt; \
1794 int i; \
1795 \
1796 pt = (u64 *)__pt; \
1797 \
1798 for (i = 0; i < 512; ++i) { \
Joerg Roedel0b3fff542015-06-18 10:48:34 +02001799 /* PTE present? */ \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001800 if (!IOMMU_PTE_PRESENT(pt[i])) \
1801 continue; \
1802 \
Joerg Roedel0b3fff542015-06-18 10:48:34 +02001803 /* Large PTE? */ \
1804 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1805 PM_PTE_LEVEL(pt[i]) == 7) \
1806 continue; \
1807 \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001808 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1809 FN(p); \
1810 } \
1811 free_page((unsigned long)pt); \
1812}
1813
1814DEFINE_FREE_PT_FN(l2, free_page)
1815DEFINE_FREE_PT_FN(l3, free_pt_l2)
1816DEFINE_FREE_PT_FN(l4, free_pt_l3)
1817DEFINE_FREE_PT_FN(l5, free_pt_l4)
1818DEFINE_FREE_PT_FN(l6, free_pt_l5)
1819
Joerg Roedel86db2e52008-12-02 18:20:21 +01001820static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001821{
Joerg Roedel5c34c402013-06-20 20:22:58 +02001822 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001823
Joerg Roedel5c34c402013-06-20 20:22:58 +02001824 switch (domain->mode) {
1825 case PAGE_MODE_NONE:
1826 break;
1827 case PAGE_MODE_1_LEVEL:
1828 free_page(root);
1829 break;
1830 case PAGE_MODE_2_LEVEL:
1831 free_pt_l2(root);
1832 break;
1833 case PAGE_MODE_3_LEVEL:
1834 free_pt_l3(root);
1835 break;
1836 case PAGE_MODE_4_LEVEL:
1837 free_pt_l4(root);
1838 break;
1839 case PAGE_MODE_5_LEVEL:
1840 free_pt_l5(root);
1841 break;
1842 case PAGE_MODE_6_LEVEL:
1843 free_pt_l6(root);
1844 break;
1845 default:
1846 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02001847 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001848}
1849
Joerg Roedelb16137b2011-11-21 16:50:23 +01001850static void free_gcr3_tbl_level1(u64 *tbl)
1851{
1852 u64 *ptr;
1853 int i;
1854
1855 for (i = 0; i < 512; ++i) {
1856 if (!(tbl[i] & GCR3_VALID))
1857 continue;
1858
1859 ptr = __va(tbl[i] & PAGE_MASK);
1860
1861 free_page((unsigned long)ptr);
1862 }
1863}
1864
1865static void free_gcr3_tbl_level2(u64 *tbl)
1866{
1867 u64 *ptr;
1868 int i;
1869
1870 for (i = 0; i < 512; ++i) {
1871 if (!(tbl[i] & GCR3_VALID))
1872 continue;
1873
1874 ptr = __va(tbl[i] & PAGE_MASK);
1875
1876 free_gcr3_tbl_level1(ptr);
1877 }
1878}
1879
Joerg Roedel52815b72011-11-17 17:24:28 +01001880static void free_gcr3_table(struct protection_domain *domain)
1881{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001882 if (domain->glx == 2)
1883 free_gcr3_tbl_level2(domain->gcr3_tbl);
1884 else if (domain->glx == 1)
1885 free_gcr3_tbl_level1(domain->gcr3_tbl);
Joerg Roedel23d3a982015-08-13 11:15:13 +02001886 else
1887 BUG_ON(domain->glx != 0);
Joerg Roedelb16137b2011-11-21 16:50:23 +01001888
Joerg Roedel52815b72011-11-17 17:24:28 +01001889 free_page((unsigned long)domain->gcr3_tbl);
1890}
1891
Joerg Roedel431b2a22008-07-11 17:14:22 +02001892/*
1893 * Free a domain, only used if something went wrong in the
1894 * allocation path and we need to free an already allocated page table
1895 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001896static void dma_ops_domain_free(struct dma_ops_domain *dom)
1897{
Joerg Roedel384de722009-05-15 12:30:05 +02001898 int i;
1899
Joerg Roedelec487d12008-06-26 21:27:58 +02001900 if (!dom)
1901 return;
1902
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001903 free_percpu(dom->next_index);
1904
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001905 del_domain_from_list(&dom->domain);
1906
Joerg Roedel86db2e52008-12-02 18:20:21 +01001907 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001908
Joerg Roedel384de722009-05-15 12:30:05 +02001909 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1910 if (!dom->aperture[i])
1911 continue;
1912 free_page((unsigned long)dom->aperture[i]->bitmap);
1913 kfree(dom->aperture[i]);
1914 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001915
1916 kfree(dom);
1917}
1918
Joerg Roedela639a8e2015-12-22 16:06:49 +01001919static int dma_ops_domain_alloc_apertures(struct dma_ops_domain *dma_dom,
1920 int max_apertures)
1921{
1922 int ret, i, apertures;
1923
1924 apertures = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
1925 ret = 0;
1926
1927 for (i = apertures; i < max_apertures; ++i) {
1928 ret = alloc_new_range(dma_dom, false, GFP_KERNEL);
1929 if (ret)
1930 break;
1931 }
1932
1933 return ret;
1934}
1935
Joerg Roedel431b2a22008-07-11 17:14:22 +02001936/*
1937 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001938 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001939 * structures required for the dma_ops interface
1940 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001941static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001942{
1943 struct dma_ops_domain *dma_dom;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001944 int cpu;
Joerg Roedelec487d12008-06-26 21:27:58 +02001945
1946 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1947 if (!dma_dom)
1948 return NULL;
1949
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001950 if (protection_domain_init(&dma_dom->domain))
Joerg Roedelec487d12008-06-26 21:27:58 +02001951 goto free_dma_dom;
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001952
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001953 dma_dom->next_index = alloc_percpu(u32);
1954 if (!dma_dom->next_index)
1955 goto free_dma_dom;
1956
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001957 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001958 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001959 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001960 dma_dom->domain.priv = dma_dom;
1961 if (!dma_dom->domain.pt_root)
1962 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001963
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001964 add_domain_to_list(&dma_dom->domain);
1965
Joerg Roedel576175c2009-11-23 19:08:46 +01001966 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001967 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001968
Joerg Roedel431b2a22008-07-11 17:14:22 +02001969 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001970 * mark the first page as allocated so we never return 0 as
1971 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001972 */
Joerg Roedel384de722009-05-15 12:30:05 +02001973 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedelec487d12008-06-26 21:27:58 +02001974
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001975 for_each_possible_cpu(cpu)
1976 *per_cpu_ptr(dma_dom->next_index, cpu) = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001977
1978 return dma_dom;
1979
1980free_dma_dom:
1981 dma_ops_domain_free(dma_dom);
1982
1983 return NULL;
1984}
1985
Joerg Roedel431b2a22008-07-11 17:14:22 +02001986/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001987 * little helper function to check whether a given protection domain is a
1988 * dma_ops domain
1989 */
1990static bool dma_ops_domain(struct protection_domain *domain)
1991{
1992 return domain->flags & PD_DMA_OPS_MASK;
1993}
1994
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001995static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001996{
Joerg Roedel132bd682011-11-17 14:18:46 +01001997 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001998 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001999
Joerg Roedel132bd682011-11-17 14:18:46 +01002000 if (domain->mode != PAGE_MODE_NONE)
2001 pte_root = virt_to_phys(domain->pt_root);
2002
Joerg Roedel38ddf412008-09-11 10:38:32 +02002003 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
2004 << DEV_ENTRY_MODE_SHIFT;
2005 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002006
Joerg Roedelee6c2862011-11-09 12:06:03 +01002007 flags = amd_iommu_dev_table[devid].data[1];
2008
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002009 if (ats)
2010 flags |= DTE_FLAG_IOTLB;
2011
Joerg Roedel52815b72011-11-17 17:24:28 +01002012 if (domain->flags & PD_IOMMUV2_MASK) {
2013 u64 gcr3 = __pa(domain->gcr3_tbl);
2014 u64 glx = domain->glx;
2015 u64 tmp;
2016
2017 pte_root |= DTE_FLAG_GV;
2018 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
2019
2020 /* First mask out possible old values for GCR3 table */
2021 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
2022 flags &= ~tmp;
2023
2024 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
2025 flags &= ~tmp;
2026
2027 /* Encode GCR3 table into DTE */
2028 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
2029 pte_root |= tmp;
2030
2031 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
2032 flags |= tmp;
2033
2034 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2035 flags |= tmp;
2036 }
2037
Joerg Roedelee6c2862011-11-09 12:06:03 +01002038 flags &= ~(0xffffUL);
2039 flags |= domain->id;
2040
2041 amd_iommu_dev_table[devid].data[1] = flags;
2042 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002043}
2044
Joerg Roedel15898bb2009-11-24 15:39:42 +01002045static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002046{
Joerg Roedel355bf552008-12-08 12:02:41 +01002047 /* remove entry from the device table seen by the hardware */
Joerg Roedelcbf3ccd2015-10-20 14:59:36 +02002048 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2049 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
Joerg Roedel355bf552008-12-08 12:02:41 +01002050
Joerg Roedelc5cca142009-10-09 18:31:20 +02002051 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002052}
2053
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002054static void do_attach(struct iommu_dev_data *dev_data,
2055 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002056{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002057 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02002058 u16 alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002059 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002060
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002061 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02002062 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002063 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002064
2065 /* Update data structures */
2066 dev_data->domain = domain;
2067 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002068
2069 /* Do reference counting */
2070 domain->dev_iommu[iommu->index] += 1;
2071 domain->dev_cnt += 1;
2072
Joerg Roedele25bfb52015-10-20 17:33:38 +02002073 /* Update device table */
2074 set_dte_entry(dev_data->devid, domain, ats);
2075 if (alias != dev_data->devid)
Baoquan He9b1a12d2016-01-20 22:01:19 +08002076 set_dte_entry(alias, domain, ats);
Joerg Roedele25bfb52015-10-20 17:33:38 +02002077
Joerg Roedel6c542042011-06-09 17:07:31 +02002078 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002079}
2080
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002081static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002082{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002083 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02002084 u16 alias;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002085
Joerg Roedel5adad992015-10-09 16:23:33 +02002086 /*
2087 * First check if the device is still attached. It might already
2088 * be detached from its domain because the generic
2089 * iommu_detach_group code detached it and we try again here in
2090 * our alias handling.
2091 */
2092 if (!dev_data->domain)
2093 return;
2094
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002095 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02002096 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002097
Joerg Roedelc4596112009-11-20 14:57:32 +01002098 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002099 dev_data->domain->dev_iommu[iommu->index] -= 1;
2100 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002101
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002102 /* Update data structures */
2103 dev_data->domain = NULL;
2104 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002105 clear_dte_entry(dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02002106 if (alias != dev_data->devid)
2107 clear_dte_entry(alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002108
2109 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002110 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002111}
2112
2113/*
2114 * If a device is not yet associated with a domain, this function does
2115 * assigns it visible for the hardware
2116 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002117static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002118 struct protection_domain *domain)
2119{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002120 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002121
Joerg Roedel272e4f92015-10-20 17:33:37 +02002122 /*
2123 * Must be called with IRQs disabled. Warn here to detect early
2124 * when its not.
2125 */
2126 WARN_ON(!irqs_disabled());
2127
Joerg Roedel15898bb2009-11-24 15:39:42 +01002128 /* lock domain */
2129 spin_lock(&domain->lock);
2130
Joerg Roedel397111a2014-08-05 17:31:51 +02002131 ret = -EBUSY;
Joerg Roedel150952f2015-10-20 17:33:35 +02002132 if (dev_data->domain != NULL)
Joerg Roedel397111a2014-08-05 17:31:51 +02002133 goto out_unlock;
Joerg Roedel24100052009-11-25 15:59:57 +01002134
Joerg Roedel397111a2014-08-05 17:31:51 +02002135 /* Attach alias group root */
Joerg Roedel150952f2015-10-20 17:33:35 +02002136 do_attach(dev_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002137
Julia Lawall84fe6c12010-05-27 12:31:51 +02002138 ret = 0;
2139
2140out_unlock:
2141
Joerg Roedel355bf552008-12-08 12:02:41 +01002142 /* ready */
2143 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002144
Julia Lawall84fe6c12010-05-27 12:31:51 +02002145 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002146}
2147
Joerg Roedel52815b72011-11-17 17:24:28 +01002148
2149static void pdev_iommuv2_disable(struct pci_dev *pdev)
2150{
2151 pci_disable_ats(pdev);
2152 pci_disable_pri(pdev);
2153 pci_disable_pasid(pdev);
2154}
2155
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002156/* FIXME: Change generic reset-function to do the same */
2157static int pri_reset_while_enabled(struct pci_dev *pdev)
2158{
2159 u16 control;
2160 int pos;
2161
Joerg Roedel46277b72011-12-07 14:34:02 +01002162 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002163 if (!pos)
2164 return -EINVAL;
2165
Joerg Roedel46277b72011-12-07 14:34:02 +01002166 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2167 control |= PCI_PRI_CTRL_RESET;
2168 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002169
2170 return 0;
2171}
2172
Joerg Roedel52815b72011-11-17 17:24:28 +01002173static int pdev_iommuv2_enable(struct pci_dev *pdev)
2174{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002175 bool reset_enable;
2176 int reqs, ret;
2177
2178 /* FIXME: Hardcode number of outstanding requests for now */
2179 reqs = 32;
2180 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2181 reqs = 1;
2182 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002183
2184 /* Only allow access to user-accessible pages */
2185 ret = pci_enable_pasid(pdev, 0);
2186 if (ret)
2187 goto out_err;
2188
2189 /* First reset the PRI state of the device */
2190 ret = pci_reset_pri(pdev);
2191 if (ret)
2192 goto out_err;
2193
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002194 /* Enable PRI */
2195 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002196 if (ret)
2197 goto out_err;
2198
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002199 if (reset_enable) {
2200 ret = pri_reset_while_enabled(pdev);
2201 if (ret)
2202 goto out_err;
2203 }
2204
Joerg Roedel52815b72011-11-17 17:24:28 +01002205 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2206 if (ret)
2207 goto out_err;
2208
2209 return 0;
2210
2211out_err:
2212 pci_disable_pri(pdev);
2213 pci_disable_pasid(pdev);
2214
2215 return ret;
2216}
2217
Joerg Roedelc99afa22011-11-21 18:19:25 +01002218/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002219#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002220
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002221static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002222{
Joerg Roedela3b93122012-04-12 12:49:26 +02002223 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002224 int pos;
2225
Joerg Roedel46277b72011-12-07 14:34:02 +01002226 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002227 if (!pos)
2228 return false;
2229
Joerg Roedela3b93122012-04-12 12:49:26 +02002230 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002231
Joerg Roedela3b93122012-04-12 12:49:26 +02002232 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002233}
2234
Joerg Roedel15898bb2009-11-24 15:39:42 +01002235/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002236 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002237 * assigns it visible for the hardware
2238 */
2239static int attach_device(struct device *dev,
2240 struct protection_domain *domain)
2241{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002242 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002243 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002244 unsigned long flags;
2245 int ret;
2246
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002247 dev_data = get_dev_data(dev);
2248
Joerg Roedel52815b72011-11-17 17:24:28 +01002249 if (domain->flags & PD_IOMMUV2_MASK) {
Joerg Roedel02ca2022015-07-28 16:58:49 +02002250 if (!dev_data->passthrough)
Joerg Roedel52815b72011-11-17 17:24:28 +01002251 return -EINVAL;
2252
Joerg Roedel02ca2022015-07-28 16:58:49 +02002253 if (dev_data->iommu_v2) {
2254 if (pdev_iommuv2_enable(pdev) != 0)
2255 return -EINVAL;
Joerg Roedel52815b72011-11-17 17:24:28 +01002256
Joerg Roedel02ca2022015-07-28 16:58:49 +02002257 dev_data->ats.enabled = true;
2258 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2259 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
2260 }
Joerg Roedel52815b72011-11-17 17:24:28 +01002261 } else if (amd_iommu_iotlb_sup &&
2262 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002263 dev_data->ats.enabled = true;
2264 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2265 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002266
Joerg Roedel15898bb2009-11-24 15:39:42 +01002267 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002268 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002269 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2270
2271 /*
2272 * We might boot into a crash-kernel here. The crashed kernel
2273 * left the caches in the IOMMU dirty. So we have to flush
2274 * here to evict all dirty stuff.
2275 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002276 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002277
2278 return ret;
2279}
2280
2281/*
2282 * Removes a device from a protection domain (unlocked)
2283 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002284static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002285{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002286 struct protection_domain *domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002287
Joerg Roedel272e4f92015-10-20 17:33:37 +02002288 /*
2289 * Must be called with IRQs disabled. Warn here to detect early
2290 * when its not.
2291 */
2292 WARN_ON(!irqs_disabled());
2293
Joerg Roedelf34c73f2015-10-20 17:33:34 +02002294 if (WARN_ON(!dev_data->domain))
2295 return;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002296
Joerg Roedel2ca76272010-01-22 16:45:31 +01002297 domain = dev_data->domain;
2298
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002299 spin_lock(&domain->lock);
Joerg Roedel24100052009-11-25 15:59:57 +01002300
Joerg Roedel150952f2015-10-20 17:33:35 +02002301 do_detach(dev_data);
Joerg Roedel71f77582011-06-09 19:03:15 +02002302
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002303 spin_unlock(&domain->lock);
Joerg Roedel355bf552008-12-08 12:02:41 +01002304}
2305
2306/*
2307 * Removes a device from a protection domain (with devtable_lock held)
2308 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002309static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002310{
Joerg Roedel52815b72011-11-17 17:24:28 +01002311 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002312 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002313 unsigned long flags;
2314
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002315 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002316 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002317
Joerg Roedel355bf552008-12-08 12:02:41 +01002318 /* lock device table */
2319 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002320 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002321 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002322
Joerg Roedel02ca2022015-07-28 16:58:49 +02002323 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
Joerg Roedel52815b72011-11-17 17:24:28 +01002324 pdev_iommuv2_disable(to_pci_dev(dev));
2325 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002326 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002327
2328 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002329}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002330
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002331static int amd_iommu_add_device(struct device *dev)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002332{
Joerg Roedel71f77582011-06-09 19:03:15 +02002333 struct iommu_dev_data *dev_data;
Joerg Roedel07ee8692015-05-28 18:41:42 +02002334 struct iommu_domain *domain;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002335 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002336 int ret, devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002337
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002338 if (!check_device(dev) || get_dev_data(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002339 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002340
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002341 devid = get_device_id(dev);
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002342 if (IS_ERR_VALUE(devid))
2343 return devid;
2344
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002345 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01002346
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002347 ret = iommu_init_device(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002348 if (ret) {
2349 if (ret != -ENOTSUPP)
2350 pr_err("Failed to initialize device %s - trying to proceed anyway\n",
2351 dev_name(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +01002352
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002353 iommu_ignore_device(dev);
Joerg Roedel343e9ca2015-05-28 18:41:43 +02002354 dev->archdata.dma_ops = &nommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002355 goto out;
2356 }
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002357 init_iommu_group(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002358
Joerg Roedel07ee8692015-05-28 18:41:42 +02002359 dev_data = get_dev_data(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002360
2361 BUG_ON(!dev_data);
2362
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002363 if (iommu_pass_through || dev_data->iommu_v2)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002364 iommu_request_dm_for_dev(dev);
2365
2366 /* Domains are initialized for this device - have a look what we ended up with */
2367 domain = iommu_get_domain_for_dev(dev);
Joerg Roedel32302322015-07-28 16:58:50 +02002368 if (domain->type == IOMMU_DOMAIN_IDENTITY)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002369 dev_data->passthrough = true;
Joerg Roedel32302322015-07-28 16:58:50 +02002370 else
Joerg Roedel07ee8692015-05-28 18:41:42 +02002371 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002372
2373out:
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002374 iommu_completion_wait(iommu);
2375
Joerg Roedele275a2a2008-12-10 18:27:25 +01002376 return 0;
2377}
2378
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002379static void amd_iommu_remove_device(struct device *dev)
Joerg Roedel8638c492009-12-10 11:12:25 +01002380{
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002381 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002382 int devid;
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002383
2384 if (!check_device(dev))
2385 return;
2386
2387 devid = get_device_id(dev);
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002388 if (IS_ERR_VALUE(devid))
2389 return;
2390
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002391 iommu = amd_iommu_rlookup_table[devid];
2392
2393 iommu_uninit_device(dev);
2394 iommu_completion_wait(iommu);
Joerg Roedel8638c492009-12-10 11:12:25 +01002395}
2396
Joerg Roedel431b2a22008-07-11 17:14:22 +02002397/*****************************************************************************
2398 *
2399 * The next functions belong to the dma_ops mapping/unmapping code.
2400 *
2401 *****************************************************************************/
2402
2403/*
2404 * In the dma_ops path we only have the struct device. This function
2405 * finds the corresponding IOMMU, the protection domain and the
2406 * requestor id for a given device.
2407 * If the device is not yet associated with a domain this is also done
2408 * in this function.
2409 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002410static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002411{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002412 struct protection_domain *domain;
Joerg Roedel063071d2015-05-28 18:41:38 +02002413 struct iommu_domain *io_domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002414
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002415 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002416 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002417
Joerg Roedel063071d2015-05-28 18:41:38 +02002418 io_domain = iommu_get_domain_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002419 if (!io_domain)
2420 return NULL;
Joerg Roedel063071d2015-05-28 18:41:38 +02002421
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002422 domain = to_pdomain(io_domain);
2423 if (!dma_ops_domain(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002424 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002425
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002426 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002427}
2428
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002429static void update_device_table(struct protection_domain *domain)
2430{
Joerg Roedel492667d2009-11-27 13:25:47 +01002431 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002432
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002433 list_for_each_entry(dev_data, &domain->dev_list, list)
2434 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002435}
2436
2437static void update_domain(struct protection_domain *domain)
2438{
2439 if (!domain->updated)
2440 return;
2441
2442 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002443
2444 domain_flush_devices(domain);
2445 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002446
2447 domain->updated = false;
2448}
2449
Joerg Roedel431b2a22008-07-11 17:14:22 +02002450/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002451 * This function fetches the PTE for a given address in the aperture
2452 */
2453static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2454 unsigned long address)
2455{
Joerg Roedel384de722009-05-15 12:30:05 +02002456 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002457 u64 *pte, *pte_page;
2458
Joerg Roedel384de722009-05-15 12:30:05 +02002459 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2460 if (!aperture)
2461 return NULL;
2462
2463 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002464 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002465 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002466 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002467 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2468 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002469 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002470
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002471 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002472
2473 return pte;
2474}
2475
2476/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002477 * This is the generic map function. It maps one 4kb page at paddr to
2478 * the given address in the DMA address space for the domain.
2479 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002480static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002481 unsigned long address,
2482 phys_addr_t paddr,
2483 int direction)
2484{
2485 u64 *pte, __pte;
2486
2487 WARN_ON(address > dom->aperture_size);
2488
2489 paddr &= PAGE_MASK;
2490
Joerg Roedel8bda3092009-05-12 12:02:46 +02002491 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002492 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002493 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002494
2495 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2496
2497 if (direction == DMA_TO_DEVICE)
2498 __pte |= IOMMU_PTE_IR;
2499 else if (direction == DMA_FROM_DEVICE)
2500 __pte |= IOMMU_PTE_IW;
2501 else if (direction == DMA_BIDIRECTIONAL)
2502 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2503
Joerg Roedela7fb6682015-12-21 12:50:54 +01002504 WARN_ON_ONCE(*pte);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002505
2506 *pte = __pte;
2507
2508 return (dma_addr_t)address;
2509}
2510
Joerg Roedel431b2a22008-07-11 17:14:22 +02002511/*
2512 * The generic unmapping function for on page in the DMA address space.
2513 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002514static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002515 unsigned long address)
2516{
Joerg Roedel384de722009-05-15 12:30:05 +02002517 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002518 u64 *pte;
2519
2520 if (address >= dom->aperture_size)
2521 return;
2522
Joerg Roedel384de722009-05-15 12:30:05 +02002523 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2524 if (!aperture)
2525 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002526
Joerg Roedel384de722009-05-15 12:30:05 +02002527 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2528 if (!pte)
2529 return;
2530
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002531 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002532
Joerg Roedela7fb6682015-12-21 12:50:54 +01002533 WARN_ON_ONCE(!*pte);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002534
2535 *pte = 0ULL;
2536}
2537
Joerg Roedel431b2a22008-07-11 17:14:22 +02002538/*
2539 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002540 * contiguous memory region into DMA address space. It is used by all
2541 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002542 * Must be called with the domain lock held.
2543 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002544static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002545 struct dma_ops_domain *dma_dom,
2546 phys_addr_t paddr,
2547 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002548 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002549 bool align,
2550 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002551{
2552 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002553 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002554 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002555 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002556 int i;
2557
Joerg Roedele3c449f2008-10-15 22:02:11 -07002558 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002559 paddr &= PAGE_MASK;
2560
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002561 INC_STATS_COUNTER(total_map_requests);
2562
Joerg Roedelc1858972008-12-12 15:42:39 +01002563 if (pages > 1)
2564 INC_STATS_COUNTER(cross_page);
2565
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002566 if (align)
2567 align_mask = (1UL << get_order(size)) - 1;
2568
Joerg Roedel832a90c2008-09-18 15:54:23 +02002569 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2570 dma_mask);
Joerg Roedelebaecb42015-12-21 18:11:32 +01002571
Joerg Roedel266a3bd2015-12-21 18:54:24 +01002572 if (address == DMA_ERROR_CODE)
2573 goto out;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002574
2575 start = address;
2576 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002577 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002578 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002579 goto out_unmap;
2580
Joerg Roedelcb76c322008-06-26 21:28:00 +02002581 paddr += PAGE_SIZE;
2582 start += PAGE_SIZE;
2583 }
2584 address += offset;
2585
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002586 ADD_STATS_COUNTER(alloced_io_mem, size);
2587
Joerg Roedelab7032b2015-12-21 18:47:11 +01002588 if (unlikely(amd_iommu_np_cache)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002589 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedelab7032b2015-12-21 18:47:11 +01002590 domain_flush_complete(&dma_dom->domain);
2591 }
Joerg Roedel270cab242008-09-04 15:49:46 +02002592
Joerg Roedelcb76c322008-06-26 21:28:00 +02002593out:
2594 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002595
2596out_unmap:
2597
2598 for (--i; i >= 0; --i) {
2599 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002600 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002601 }
2602
2603 dma_ops_free_addresses(dma_dom, address, pages);
2604
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002605 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002606}
2607
Joerg Roedel431b2a22008-07-11 17:14:22 +02002608/*
2609 * Does the reverse of the __map_single function. Must be called with
2610 * the domain lock held too
2611 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002612static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002613 dma_addr_t dma_addr,
2614 size_t size,
2615 int dir)
2616{
Joerg Roedel04e04632010-09-23 16:12:48 +02002617 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002618 dma_addr_t i, start;
2619 unsigned int pages;
2620
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002621 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002622 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002623 return;
2624
Joerg Roedel04e04632010-09-23 16:12:48 +02002625 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002626 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002627 dma_addr &= PAGE_MASK;
2628 start = dma_addr;
2629
2630 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002631 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002632 start += PAGE_SIZE;
2633 }
2634
Joerg Roedel84b3a0b2015-12-21 13:23:59 +01002635 SUB_STATS_COUNTER(alloced_io_mem, size);
2636
2637 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002638}
2639
Joerg Roedel431b2a22008-07-11 17:14:22 +02002640/*
2641 * The exported map_single function for dma_ops.
2642 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002643static dma_addr_t map_page(struct device *dev, struct page *page,
2644 unsigned long offset, size_t size,
2645 enum dma_data_direction dir,
2646 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002647{
FUJITA Tomonori51491362009-01-05 23:47:25 +09002648 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002649 struct protection_domain *domain;
2650 u64 dma_mask;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002651
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002652 INC_STATS_COUNTER(cnt_map_single);
2653
Joerg Roedel94f6d192009-11-24 16:40:02 +01002654 domain = get_domain(dev);
2655 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002656 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002657 else if (IS_ERR(domain))
2658 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002659
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002660 dma_mask = *dev->dma_mask;
2661
Joerg Roedel92d420e2015-12-21 19:31:33 +01002662 return __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002663 dma_mask);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002664}
2665
Joerg Roedel431b2a22008-07-11 17:14:22 +02002666/*
2667 * The exported unmap_single function for dma_ops.
2668 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002669static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2670 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002671{
Joerg Roedel4da70b92008-06-26 21:28:01 +02002672 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002673
Joerg Roedel146a6912008-12-12 15:07:12 +01002674 INC_STATS_COUNTER(cnt_unmap_single);
2675
Joerg Roedel94f6d192009-11-24 16:40:02 +01002676 domain = get_domain(dev);
2677 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002678 return;
2679
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002680 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002681}
2682
Joerg Roedel431b2a22008-07-11 17:14:22 +02002683/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002684 * The exported map_sg function for dma_ops (handles scatter-gather
2685 * lists).
2686 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002687static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002688 int nelems, enum dma_data_direction dir,
2689 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002690{
Joerg Roedel65b050a2008-06-26 21:28:02 +02002691 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002692 int i;
2693 struct scatterlist *s;
2694 phys_addr_t paddr;
2695 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002696 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002697
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002698 INC_STATS_COUNTER(cnt_map_sg);
2699
Joerg Roedel94f6d192009-11-24 16:40:02 +01002700 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002701 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002702 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002703
Joerg Roedel832a90c2008-09-18 15:54:23 +02002704 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002705
Joerg Roedel65b050a2008-06-26 21:28:02 +02002706 for_each_sg(sglist, s, nelems, i) {
2707 paddr = sg_phys(s);
2708
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002709 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002710 paddr, s->length, dir, false,
2711 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002712
2713 if (s->dma_address) {
2714 s->dma_length = s->length;
2715 mapped_elems++;
2716 } else
2717 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002718 }
2719
Joerg Roedel65b050a2008-06-26 21:28:02 +02002720 return mapped_elems;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002721
Joerg Roedel65b050a2008-06-26 21:28:02 +02002722unmap:
2723 for_each_sg(sglist, s, mapped_elems, i) {
2724 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002725 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002726 s->dma_length, dir);
2727 s->dma_address = s->dma_length = 0;
2728 }
2729
Joerg Roedel92d420e2015-12-21 19:31:33 +01002730 return 0;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002731}
2732
Joerg Roedel431b2a22008-07-11 17:14:22 +02002733/*
2734 * The exported map_sg function for dma_ops (handles scatter-gather
2735 * lists).
2736 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002737static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002738 int nelems, enum dma_data_direction dir,
2739 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002740{
Joerg Roedel65b050a2008-06-26 21:28:02 +02002741 struct protection_domain *domain;
2742 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002743 int i;
2744
Joerg Roedel55877a62008-12-12 15:12:14 +01002745 INC_STATS_COUNTER(cnt_unmap_sg);
2746
Joerg Roedel94f6d192009-11-24 16:40:02 +01002747 domain = get_domain(dev);
2748 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002749 return;
2750
Joerg Roedel65b050a2008-06-26 21:28:02 +02002751 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002752 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002753 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002754 s->dma_address = s->dma_length = 0;
2755 }
Joerg Roedel65b050a2008-06-26 21:28:02 +02002756}
2757
Joerg Roedel431b2a22008-07-11 17:14:22 +02002758/*
2759 * The exported alloc_coherent function for dma_ops.
2760 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002761static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002762 dma_addr_t *dma_addr, gfp_t flag,
2763 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002764{
Joerg Roedel832a90c2008-09-18 15:54:23 +02002765 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002766 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002767 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002768
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002769 INC_STATS_COUNTER(cnt_alloc_coherent);
2770
Joerg Roedel94f6d192009-11-24 16:40:02 +01002771 domain = get_domain(dev);
2772 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedel3b839a52015-04-01 14:58:47 +02002773 page = alloc_pages(flag, get_order(size));
2774 *dma_addr = page_to_phys(page);
2775 return page_address(page);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002776 } else if (IS_ERR(domain))
2777 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002778
Joerg Roedel3b839a52015-04-01 14:58:47 +02002779 size = PAGE_ALIGN(size);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002780 dma_mask = dev->coherent_dma_mask;
2781 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
Joerg Roedel2d0ec7a2015-06-01 17:30:57 +02002782 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002783
Joerg Roedel3b839a52015-04-01 14:58:47 +02002784 page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
2785 if (!page) {
Mel Gormand0164ad2015-11-06 16:28:21 -08002786 if (!gfpflags_allow_blocking(flag))
Joerg Roedel3b839a52015-04-01 14:58:47 +02002787 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002788
Joerg Roedel3b839a52015-04-01 14:58:47 +02002789 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
2790 get_order(size));
2791 if (!page)
2792 return NULL;
2793 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002794
Joerg Roedel832a90c2008-09-18 15:54:23 +02002795 if (!dma_mask)
2796 dma_mask = *dev->dma_mask;
2797
Joerg Roedel3b839a52015-04-01 14:58:47 +02002798 *dma_addr = __map_single(dev, domain->priv, page_to_phys(page),
Joerg Roedel832a90c2008-09-18 15:54:23 +02002799 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002800
Joerg Roedel92d420e2015-12-21 19:31:33 +01002801 if (*dma_addr == DMA_ERROR_CODE)
Joerg Roedel5b28df62008-12-02 17:49:42 +01002802 goto out_free;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002803
Joerg Roedel3b839a52015-04-01 14:58:47 +02002804 return page_address(page);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002805
2806out_free:
2807
Joerg Roedel3b839a52015-04-01 14:58:47 +02002808 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2809 __free_pages(page, get_order(size));
Joerg Roedel5b28df62008-12-02 17:49:42 +01002810
2811 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002812}
2813
Joerg Roedel431b2a22008-07-11 17:14:22 +02002814/*
2815 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002816 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002817static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002818 void *virt_addr, dma_addr_t dma_addr,
2819 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002820{
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002821 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002822 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002823
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002824 INC_STATS_COUNTER(cnt_free_coherent);
2825
Joerg Roedel3b839a52015-04-01 14:58:47 +02002826 page = virt_to_page(virt_addr);
2827 size = PAGE_ALIGN(size);
2828
Joerg Roedel94f6d192009-11-24 16:40:02 +01002829 domain = get_domain(dev);
2830 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002831 goto free_mem;
2832
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002833 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002834
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002835free_mem:
Joerg Roedel3b839a52015-04-01 14:58:47 +02002836 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2837 __free_pages(page, get_order(size));
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002838}
2839
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002840/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002841 * This function is called by the DMA layer to find out if we can handle a
2842 * particular device. It is part of the dma_ops.
2843 */
2844static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2845{
Joerg Roedel420aef82009-11-23 16:14:57 +01002846 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002847}
2848
Joerg Roedela639a8e2015-12-22 16:06:49 +01002849static int set_dma_mask(struct device *dev, u64 mask)
2850{
2851 struct protection_domain *domain;
2852 int max_apertures = 1;
2853
2854 domain = get_domain(dev);
2855 if (IS_ERR(domain))
2856 return PTR_ERR(domain);
2857
2858 if (mask == DMA_BIT_MASK(64))
2859 max_apertures = 8;
2860 else if (mask > DMA_BIT_MASK(32))
2861 max_apertures = 4;
2862
2863 /*
2864 * To prevent lock contention it doesn't make sense to allocate more
2865 * apertures than online cpus
2866 */
2867 if (max_apertures > num_online_cpus())
2868 max_apertures = num_online_cpus();
2869
2870 if (dma_ops_domain_alloc_apertures(domain->priv, max_apertures))
2871 dev_err(dev, "Can't allocate %d iommu apertures\n",
2872 max_apertures);
2873
2874 return 0;
2875}
2876
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002877static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedela639a8e2015-12-22 16:06:49 +01002878 .alloc = alloc_coherent,
2879 .free = free_coherent,
2880 .map_page = map_page,
2881 .unmap_page = unmap_page,
2882 .map_sg = map_sg,
2883 .unmap_sg = unmap_sg,
2884 .dma_supported = amd_iommu_dma_supported,
2885 .set_dma_mask = set_dma_mask,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002886};
2887
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002888int __init amd_iommu_init_api(void)
Joerg Roedel27c21272011-05-30 15:56:24 +02002889{
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002890 return bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01002891}
2892
Joerg Roedel6631ee92008-06-26 21:28:05 +02002893int __init amd_iommu_init_dma_ops(void)
2894{
Joerg Roedel32302322015-07-28 16:58:50 +02002895 swiotlb = iommu_pass_through ? 1 : 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002896 iommu_detected = 1;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002897
Joerg Roedel52717822015-07-28 16:58:51 +02002898 /*
2899 * In case we don't initialize SWIOTLB (actually the common case
2900 * when AMD IOMMU is enabled), make sure there are global
2901 * dma_ops set as a fall-back for devices not handled by this
2902 * driver (for example non-PCI devices).
2903 */
2904 if (!swiotlb)
2905 dma_ops = &nommu_dma_ops;
2906
Joerg Roedel7f265082008-12-12 13:50:21 +01002907 amd_iommu_stats_init();
2908
Joerg Roedel62410ee2012-06-12 16:42:43 +02002909 if (amd_iommu_unmap_flush)
2910 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
2911 else
2912 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
2913
Joerg Roedel6631ee92008-06-26 21:28:05 +02002914 return 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002915}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002916
2917/*****************************************************************************
2918 *
2919 * The following functions belong to the exported interface of AMD IOMMU
2920 *
2921 * This interface allows access to lower level functions of the IOMMU
2922 * like protection domain handling and assignement of devices to domains
2923 * which is not possible with the dma_ops interface.
2924 *
2925 *****************************************************************************/
2926
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002927static void cleanup_domain(struct protection_domain *domain)
2928{
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002929 struct iommu_dev_data *entry;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002930 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002931
2932 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2933
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002934 while (!list_empty(&domain->dev_list)) {
2935 entry = list_first_entry(&domain->dev_list,
2936 struct iommu_dev_data, list);
2937 __detach_device(entry);
Joerg Roedel492667d2009-11-27 13:25:47 +01002938 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002939
2940 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2941}
2942
Joerg Roedel26508152009-08-26 16:52:40 +02002943static void protection_domain_free(struct protection_domain *domain)
2944{
2945 if (!domain)
2946 return;
2947
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002948 del_domain_from_list(domain);
2949
Joerg Roedel26508152009-08-26 16:52:40 +02002950 if (domain->id)
2951 domain_id_free(domain->id);
2952
2953 kfree(domain);
2954}
2955
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002956static int protection_domain_init(struct protection_domain *domain)
2957{
2958 spin_lock_init(&domain->lock);
2959 mutex_init(&domain->api_lock);
2960 domain->id = domain_id_alloc();
2961 if (!domain->id)
2962 return -ENOMEM;
2963 INIT_LIST_HEAD(&domain->dev_list);
2964
2965 return 0;
2966}
2967
Joerg Roedel26508152009-08-26 16:52:40 +02002968static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002969{
2970 struct protection_domain *domain;
2971
2972 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2973 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002974 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002975
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002976 if (protection_domain_init(domain))
Joerg Roedel26508152009-08-26 16:52:40 +02002977 goto out_err;
2978
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002979 add_domain_to_list(domain);
2980
Joerg Roedel26508152009-08-26 16:52:40 +02002981 return domain;
2982
2983out_err:
2984 kfree(domain);
2985
2986 return NULL;
2987}
2988
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002989static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2990{
2991 struct protection_domain *pdomain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002992 struct dma_ops_domain *dma_domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002993
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002994 switch (type) {
2995 case IOMMU_DOMAIN_UNMANAGED:
2996 pdomain = protection_domain_alloc();
2997 if (!pdomain)
2998 return NULL;
2999
3000 pdomain->mode = PAGE_MODE_3_LEVEL;
3001 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3002 if (!pdomain->pt_root) {
3003 protection_domain_free(pdomain);
3004 return NULL;
3005 }
3006
3007 pdomain->domain.geometry.aperture_start = 0;
3008 pdomain->domain.geometry.aperture_end = ~0ULL;
3009 pdomain->domain.geometry.force_aperture = true;
3010
3011 break;
3012 case IOMMU_DOMAIN_DMA:
3013 dma_domain = dma_ops_domain_alloc();
3014 if (!dma_domain) {
3015 pr_err("AMD-Vi: Failed to allocate\n");
3016 return NULL;
3017 }
3018 pdomain = &dma_domain->domain;
3019 break;
Joerg Roedel07f643a2015-05-28 18:41:41 +02003020 case IOMMU_DOMAIN_IDENTITY:
3021 pdomain = protection_domain_alloc();
3022 if (!pdomain)
3023 return NULL;
3024
3025 pdomain->mode = PAGE_MODE_NONE;
3026 break;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02003027 default:
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003028 return NULL;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02003029 }
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003030
3031 return &pdomain->domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003032}
3033
3034static void amd_iommu_domain_free(struct iommu_domain *dom)
Joerg Roedel26508152009-08-26 16:52:40 +02003035{
3036 struct protection_domain *domain;
3037
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003038 if (!dom)
Joerg Roedel98383fc2008-12-02 18:34:12 +01003039 return;
3040
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003041 domain = to_pdomain(dom);
3042
Joerg Roedel98383fc2008-12-02 18:34:12 +01003043 if (domain->dev_cnt > 0)
3044 cleanup_domain(domain);
3045
3046 BUG_ON(domain->dev_cnt != 0);
3047
Joerg Roedel132bd682011-11-17 14:18:46 +01003048 if (domain->mode != PAGE_MODE_NONE)
3049 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003050
Joerg Roedel52815b72011-11-17 17:24:28 +01003051 if (domain->flags & PD_IOMMUV2_MASK)
3052 free_gcr3_table(domain);
3053
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003054 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003055}
3056
Joerg Roedel684f2882008-12-08 12:07:44 +01003057static void amd_iommu_detach_device(struct iommu_domain *dom,
3058 struct device *dev)
3059{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003060 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003061 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003062 int devid;
Joerg Roedel684f2882008-12-08 12:07:44 +01003063
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003064 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003065 return;
3066
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003067 devid = get_device_id(dev);
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003068 if (IS_ERR_VALUE(devid))
3069 return;
Joerg Roedel684f2882008-12-08 12:07:44 +01003070
Joerg Roedel657cbb62009-11-23 15:26:46 +01003071 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003072 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003073
3074 iommu = amd_iommu_rlookup_table[devid];
3075 if (!iommu)
3076 return;
3077
Joerg Roedel684f2882008-12-08 12:07:44 +01003078 iommu_completion_wait(iommu);
3079}
3080
Joerg Roedel01106062008-12-02 19:34:11 +01003081static int amd_iommu_attach_device(struct iommu_domain *dom,
3082 struct device *dev)
3083{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003084 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel657cbb62009-11-23 15:26:46 +01003085 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003086 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003087 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003088
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003089 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003090 return -EINVAL;
3091
Joerg Roedel657cbb62009-11-23 15:26:46 +01003092 dev_data = dev->archdata.iommu;
3093
Joerg Roedelf62dda62011-06-09 12:55:35 +02003094 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003095 if (!iommu)
3096 return -EINVAL;
3097
Joerg Roedel657cbb62009-11-23 15:26:46 +01003098 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003099 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003100
Joerg Roedel15898bb2009-11-24 15:39:42 +01003101 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003102
3103 iommu_completion_wait(iommu);
3104
Joerg Roedel15898bb2009-11-24 15:39:42 +01003105 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003106}
3107
Joerg Roedel468e2362010-01-21 16:37:36 +01003108static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003109 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003110{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003111 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003112 int prot = 0;
3113 int ret;
3114
Joerg Roedel132bd682011-11-17 14:18:46 +01003115 if (domain->mode == PAGE_MODE_NONE)
3116 return -EINVAL;
3117
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003118 if (iommu_prot & IOMMU_READ)
3119 prot |= IOMMU_PROT_IR;
3120 if (iommu_prot & IOMMU_WRITE)
3121 prot |= IOMMU_PROT_IW;
3122
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003123 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003124 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003125 mutex_unlock(&domain->api_lock);
3126
Joerg Roedel795e74f72010-05-11 17:40:57 +02003127 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003128}
3129
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003130static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3131 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003132{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003133 struct protection_domain *domain = to_pdomain(dom);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003134 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003135
Joerg Roedel132bd682011-11-17 14:18:46 +01003136 if (domain->mode == PAGE_MODE_NONE)
3137 return -EINVAL;
3138
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003139 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003140 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003141 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003142
Joerg Roedel17b124b2011-04-06 18:01:35 +02003143 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003144
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003145 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003146}
3147
Joerg Roedel645c4c82008-12-02 20:05:50 +01003148static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547ac2013-03-29 01:23:58 +05303149 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01003150{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003151 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel3039ca12015-04-01 14:58:48 +02003152 unsigned long offset_mask, pte_pgsize;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003153 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003154
Joerg Roedel132bd682011-11-17 14:18:46 +01003155 if (domain->mode == PAGE_MODE_NONE)
3156 return iova;
3157
Joerg Roedel3039ca12015-04-01 14:58:48 +02003158 pte = fetch_pte(domain, iova, &pte_pgsize);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003159
Joerg Roedela6d41a42009-09-02 17:08:55 +02003160 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003161 return 0;
3162
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003163 offset_mask = pte_pgsize - 1;
3164 __pte = *pte & PM_ADDR_MASK;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003165
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003166 return (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003167}
3168
Joerg Roedelab636482014-09-05 10:48:21 +02003169static bool amd_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003170{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003171 switch (cap) {
3172 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedelab636482014-09-05 10:48:21 +02003173 return true;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003174 case IOMMU_CAP_INTR_REMAP:
Joerg Roedelab636482014-09-05 10:48:21 +02003175 return (irq_remapping_enabled == 1);
Will Deaconcfdeec22014-10-27 11:24:48 +00003176 case IOMMU_CAP_NOEXEC:
3177 return false;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003178 }
3179
Joerg Roedelab636482014-09-05 10:48:21 +02003180 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003181}
3182
Joerg Roedel35cf2482015-05-28 18:41:37 +02003183static void amd_iommu_get_dm_regions(struct device *dev,
3184 struct list_head *head)
3185{
3186 struct unity_map_entry *entry;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003187 int devid;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003188
3189 devid = get_device_id(dev);
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003190 if (IS_ERR_VALUE(devid))
3191 return;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003192
3193 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
3194 struct iommu_dm_region *region;
3195
3196 if (devid < entry->devid_start || devid > entry->devid_end)
3197 continue;
3198
3199 region = kzalloc(sizeof(*region), GFP_KERNEL);
3200 if (!region) {
3201 pr_err("Out of memory allocating dm-regions for %s\n",
3202 dev_name(dev));
3203 return;
3204 }
3205
3206 region->start = entry->address_start;
3207 region->length = entry->address_end - entry->address_start;
3208 if (entry->prot & IOMMU_PROT_IR)
3209 region->prot |= IOMMU_READ;
3210 if (entry->prot & IOMMU_PROT_IW)
3211 region->prot |= IOMMU_WRITE;
3212
3213 list_add_tail(&region->list, head);
3214 }
3215}
3216
3217static void amd_iommu_put_dm_regions(struct device *dev,
3218 struct list_head *head)
3219{
3220 struct iommu_dm_region *entry, *next;
3221
3222 list_for_each_entry_safe(entry, next, head, list)
3223 kfree(entry);
3224}
3225
Thierry Redingb22f6432014-06-27 09:03:12 +02003226static const struct iommu_ops amd_iommu_ops = {
Joerg Roedelab636482014-09-05 10:48:21 +02003227 .capable = amd_iommu_capable,
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003228 .domain_alloc = amd_iommu_domain_alloc,
3229 .domain_free = amd_iommu_domain_free,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003230 .attach_dev = amd_iommu_attach_device,
3231 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003232 .map = amd_iommu_map,
3233 .unmap = amd_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07003234 .map_sg = default_iommu_map_sg,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003235 .iova_to_phys = amd_iommu_iova_to_phys,
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02003236 .add_device = amd_iommu_add_device,
3237 .remove_device = amd_iommu_remove_device,
Joerg Roedela960fad2015-10-21 23:51:39 +02003238 .device_group = pci_device_group,
Joerg Roedel35cf2482015-05-28 18:41:37 +02003239 .get_dm_regions = amd_iommu_get_dm_regions,
3240 .put_dm_regions = amd_iommu_put_dm_regions,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003241 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003242};
3243
Joerg Roedel0feae532009-08-26 15:26:30 +02003244/*****************************************************************************
3245 *
3246 * The next functions do a basic initialization of IOMMU for pass through
3247 * mode
3248 *
3249 * In passthrough mode the IOMMU is initialized and enabled but not used for
3250 * DMA-API translation.
3251 *
3252 *****************************************************************************/
3253
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003254/* IOMMUv2 specific functions */
3255int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3256{
3257 return atomic_notifier_chain_register(&ppr_notifier, nb);
3258}
3259EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3260
3261int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3262{
3263 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3264}
3265EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003266
3267void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3268{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003269 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel132bd682011-11-17 14:18:46 +01003270 unsigned long flags;
3271
3272 spin_lock_irqsave(&domain->lock, flags);
3273
3274 /* Update data structure */
3275 domain->mode = PAGE_MODE_NONE;
3276 domain->updated = true;
3277
3278 /* Make changes visible to IOMMUs */
3279 update_domain(domain);
3280
3281 /* Page-table is not visible to IOMMU anymore, so free it */
3282 free_pagetable(domain);
3283
3284 spin_unlock_irqrestore(&domain->lock, flags);
3285}
3286EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003287
3288int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3289{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003290 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel52815b72011-11-17 17:24:28 +01003291 unsigned long flags;
3292 int levels, ret;
3293
3294 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3295 return -EINVAL;
3296
3297 /* Number of GCR3 table levels required */
3298 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3299 levels += 1;
3300
3301 if (levels > amd_iommu_max_glx_val)
3302 return -EINVAL;
3303
3304 spin_lock_irqsave(&domain->lock, flags);
3305
3306 /*
3307 * Save us all sanity checks whether devices already in the
3308 * domain support IOMMUv2. Just force that the domain has no
3309 * devices attached when it is switched into IOMMUv2 mode.
3310 */
3311 ret = -EBUSY;
3312 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3313 goto out;
3314
3315 ret = -ENOMEM;
3316 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3317 if (domain->gcr3_tbl == NULL)
3318 goto out;
3319
3320 domain->glx = levels;
3321 domain->flags |= PD_IOMMUV2_MASK;
3322 domain->updated = true;
3323
3324 update_domain(domain);
3325
3326 ret = 0;
3327
3328out:
3329 spin_unlock_irqrestore(&domain->lock, flags);
3330
3331 return ret;
3332}
3333EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003334
3335static int __flush_pasid(struct protection_domain *domain, int pasid,
3336 u64 address, bool size)
3337{
3338 struct iommu_dev_data *dev_data;
3339 struct iommu_cmd cmd;
3340 int i, ret;
3341
3342 if (!(domain->flags & PD_IOMMUV2_MASK))
3343 return -EINVAL;
3344
3345 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3346
3347 /*
3348 * IOMMU TLB needs to be flushed before Device TLB to
3349 * prevent device TLB refill from IOMMU TLB
3350 */
3351 for (i = 0; i < amd_iommus_present; ++i) {
3352 if (domain->dev_iommu[i] == 0)
3353 continue;
3354
3355 ret = iommu_queue_command(amd_iommus[i], &cmd);
3356 if (ret != 0)
3357 goto out;
3358 }
3359
3360 /* Wait until IOMMU TLB flushes are complete */
3361 domain_flush_complete(domain);
3362
3363 /* Now flush device TLBs */
3364 list_for_each_entry(dev_data, &domain->dev_list, list) {
3365 struct amd_iommu *iommu;
3366 int qdep;
3367
Joerg Roedel1c1cc452015-07-30 11:24:45 +02003368 /*
3369 There might be non-IOMMUv2 capable devices in an IOMMUv2
3370 * domain.
3371 */
3372 if (!dev_data->ats.enabled)
3373 continue;
Joerg Roedel22e266c2011-11-21 15:59:08 +01003374
3375 qdep = dev_data->ats.qdep;
3376 iommu = amd_iommu_rlookup_table[dev_data->devid];
3377
3378 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3379 qdep, address, size);
3380
3381 ret = iommu_queue_command(iommu, &cmd);
3382 if (ret != 0)
3383 goto out;
3384 }
3385
3386 /* Wait until all device TLBs are flushed */
3387 domain_flush_complete(domain);
3388
3389 ret = 0;
3390
3391out:
3392
3393 return ret;
3394}
3395
3396static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3397 u64 address)
3398{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003399 INC_STATS_COUNTER(invalidate_iotlb);
3400
Joerg Roedel22e266c2011-11-21 15:59:08 +01003401 return __flush_pasid(domain, pasid, address, false);
3402}
3403
3404int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3405 u64 address)
3406{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003407 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003408 unsigned long flags;
3409 int ret;
3410
3411 spin_lock_irqsave(&domain->lock, flags);
3412 ret = __amd_iommu_flush_page(domain, pasid, address);
3413 spin_unlock_irqrestore(&domain->lock, flags);
3414
3415 return ret;
3416}
3417EXPORT_SYMBOL(amd_iommu_flush_page);
3418
3419static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3420{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003421 INC_STATS_COUNTER(invalidate_iotlb_all);
3422
Joerg Roedel22e266c2011-11-21 15:59:08 +01003423 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3424 true);
3425}
3426
3427int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3428{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003429 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003430 unsigned long flags;
3431 int ret;
3432
3433 spin_lock_irqsave(&domain->lock, flags);
3434 ret = __amd_iommu_flush_tlb(domain, pasid);
3435 spin_unlock_irqrestore(&domain->lock, flags);
3436
3437 return ret;
3438}
3439EXPORT_SYMBOL(amd_iommu_flush_tlb);
3440
Joerg Roedelb16137b2011-11-21 16:50:23 +01003441static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3442{
3443 int index;
3444 u64 *pte;
3445
3446 while (true) {
3447
3448 index = (pasid >> (9 * level)) & 0x1ff;
3449 pte = &root[index];
3450
3451 if (level == 0)
3452 break;
3453
3454 if (!(*pte & GCR3_VALID)) {
3455 if (!alloc)
3456 return NULL;
3457
3458 root = (void *)get_zeroed_page(GFP_ATOMIC);
3459 if (root == NULL)
3460 return NULL;
3461
3462 *pte = __pa(root) | GCR3_VALID;
3463 }
3464
3465 root = __va(*pte & PAGE_MASK);
3466
3467 level -= 1;
3468 }
3469
3470 return pte;
3471}
3472
3473static int __set_gcr3(struct protection_domain *domain, int pasid,
3474 unsigned long cr3)
3475{
3476 u64 *pte;
3477
3478 if (domain->mode != PAGE_MODE_NONE)
3479 return -EINVAL;
3480
3481 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3482 if (pte == NULL)
3483 return -ENOMEM;
3484
3485 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3486
3487 return __amd_iommu_flush_tlb(domain, pasid);
3488}
3489
3490static int __clear_gcr3(struct protection_domain *domain, int pasid)
3491{
3492 u64 *pte;
3493
3494 if (domain->mode != PAGE_MODE_NONE)
3495 return -EINVAL;
3496
3497 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3498 if (pte == NULL)
3499 return 0;
3500
3501 *pte = 0;
3502
3503 return __amd_iommu_flush_tlb(domain, pasid);
3504}
3505
3506int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3507 unsigned long cr3)
3508{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003509 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003510 unsigned long flags;
3511 int ret;
3512
3513 spin_lock_irqsave(&domain->lock, flags);
3514 ret = __set_gcr3(domain, pasid, cr3);
3515 spin_unlock_irqrestore(&domain->lock, flags);
3516
3517 return ret;
3518}
3519EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3520
3521int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3522{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003523 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003524 unsigned long flags;
3525 int ret;
3526
3527 spin_lock_irqsave(&domain->lock, flags);
3528 ret = __clear_gcr3(domain, pasid);
3529 spin_unlock_irqrestore(&domain->lock, flags);
3530
3531 return ret;
3532}
3533EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003534
3535int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3536 int status, int tag)
3537{
3538 struct iommu_dev_data *dev_data;
3539 struct amd_iommu *iommu;
3540 struct iommu_cmd cmd;
3541
Joerg Roedel399be2f2011-12-01 16:53:47 +01003542 INC_STATS_COUNTER(complete_ppr);
3543
Joerg Roedelc99afa22011-11-21 18:19:25 +01003544 dev_data = get_dev_data(&pdev->dev);
3545 iommu = amd_iommu_rlookup_table[dev_data->devid];
3546
3547 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3548 tag, dev_data->pri_tlp);
3549
3550 return iommu_queue_command(iommu, &cmd);
3551}
3552EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003553
3554struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3555{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003556 struct protection_domain *pdomain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003557
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003558 pdomain = get_domain(&pdev->dev);
3559 if (IS_ERR(pdomain))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003560 return NULL;
3561
3562 /* Only return IOMMUv2 domains */
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003563 if (!(pdomain->flags & PD_IOMMUV2_MASK))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003564 return NULL;
3565
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003566 return &pdomain->domain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003567}
3568EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003569
3570void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3571{
3572 struct iommu_dev_data *dev_data;
3573
3574 if (!amd_iommu_v2_supported())
3575 return;
3576
3577 dev_data = get_dev_data(&pdev->dev);
3578 dev_data->errata |= (1 << erratum);
3579}
3580EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003581
3582int amd_iommu_device_info(struct pci_dev *pdev,
3583 struct amd_iommu_device_info *info)
3584{
3585 int max_pasids;
3586 int pos;
3587
3588 if (pdev == NULL || info == NULL)
3589 return -EINVAL;
3590
3591 if (!amd_iommu_v2_supported())
3592 return -EINVAL;
3593
3594 memset(info, 0, sizeof(*info));
3595
3596 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3597 if (pos)
3598 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3599
3600 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3601 if (pos)
3602 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3603
3604 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3605 if (pos) {
3606 int features;
3607
3608 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3609 max_pasids = min(max_pasids, (1 << 20));
3610
3611 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3612 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3613
3614 features = pci_pasid_features(pdev);
3615 if (features & PCI_PASID_CAP_EXEC)
3616 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3617 if (features & PCI_PASID_CAP_PRIV)
3618 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3619 }
3620
3621 return 0;
3622}
3623EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003624
3625#ifdef CONFIG_IRQ_REMAP
3626
3627/*****************************************************************************
3628 *
3629 * Interrupt Remapping Implementation
3630 *
3631 *****************************************************************************/
3632
3633union irte {
3634 u32 val;
3635 struct {
3636 u32 valid : 1,
3637 no_fault : 1,
3638 int_type : 3,
3639 rq_eoi : 1,
3640 dm : 1,
3641 rsvd_1 : 1,
3642 destination : 8,
3643 vector : 8,
3644 rsvd_2 : 8;
3645 } fields;
3646};
3647
Jiang Liu9c724962015-04-14 10:29:52 +08003648struct irq_2_irte {
3649 u16 devid; /* Device ID for IRTE table */
3650 u16 index; /* Index into IRTE table*/
3651};
3652
Jiang Liu7c71d302015-04-13 14:11:33 +08003653struct amd_ir_data {
3654 struct irq_2_irte irq_2_irte;
3655 union irte irte_entry;
3656 union {
3657 struct msi_msg msi_entry;
3658 };
3659};
3660
3661static struct irq_chip amd_ir_chip;
3662
Joerg Roedel2b324502012-06-21 16:29:10 +02003663#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3664#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3665#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3666#define DTE_IRQ_REMAP_ENABLE 1ULL
3667
3668static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3669{
3670 u64 dte;
3671
3672 dte = amd_iommu_dev_table[devid].data[2];
3673 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3674 dte |= virt_to_phys(table->table);
3675 dte |= DTE_IRQ_REMAP_INTCTL;
3676 dte |= DTE_IRQ_TABLE_LEN;
3677 dte |= DTE_IRQ_REMAP_ENABLE;
3678
3679 amd_iommu_dev_table[devid].data[2] = dte;
3680}
3681
3682#define IRTE_ALLOCATED (~1U)
3683
3684static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3685{
3686 struct irq_remap_table *table = NULL;
3687 struct amd_iommu *iommu;
3688 unsigned long flags;
3689 u16 alias;
3690
3691 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3692
3693 iommu = amd_iommu_rlookup_table[devid];
3694 if (!iommu)
3695 goto out_unlock;
3696
3697 table = irq_lookup_table[devid];
3698 if (table)
3699 goto out;
3700
3701 alias = amd_iommu_alias_table[devid];
3702 table = irq_lookup_table[alias];
3703 if (table) {
3704 irq_lookup_table[devid] = table;
3705 set_dte_irq_entry(devid, table);
3706 iommu_flush_dte(iommu, devid);
3707 goto out;
3708 }
3709
3710 /* Nothing there yet, allocate new irq remapping table */
3711 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3712 if (!table)
3713 goto out;
3714
Joerg Roedel197887f2013-04-09 21:14:08 +02003715 /* Initialize table spin-lock */
3716 spin_lock_init(&table->lock);
3717
Joerg Roedel2b324502012-06-21 16:29:10 +02003718 if (ioapic)
3719 /* Keep the first 32 indexes free for IOAPIC interrupts */
3720 table->min_index = 32;
3721
3722 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3723 if (!table->table) {
3724 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003725 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003726 goto out;
3727 }
3728
3729 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3730
3731 if (ioapic) {
3732 int i;
3733
3734 for (i = 0; i < 32; ++i)
3735 table->table[i] = IRTE_ALLOCATED;
3736 }
3737
3738 irq_lookup_table[devid] = table;
3739 set_dte_irq_entry(devid, table);
3740 iommu_flush_dte(iommu, devid);
3741 if (devid != alias) {
3742 irq_lookup_table[alias] = table;
Alex Williamsone028a9e2014-04-22 10:08:40 -06003743 set_dte_irq_entry(alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003744 iommu_flush_dte(iommu, alias);
3745 }
3746
3747out:
3748 iommu_completion_wait(iommu);
3749
3750out_unlock:
3751 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3752
3753 return table;
3754}
3755
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003756static int alloc_irq_index(u16 devid, int count)
Joerg Roedel2b324502012-06-21 16:29:10 +02003757{
3758 struct irq_remap_table *table;
3759 unsigned long flags;
3760 int index, c;
3761
3762 table = get_irq_table(devid, false);
3763 if (!table)
3764 return -ENODEV;
3765
3766 spin_lock_irqsave(&table->lock, flags);
3767
3768 /* Scan table for free entries */
3769 for (c = 0, index = table->min_index;
3770 index < MAX_IRQS_PER_TABLE;
3771 ++index) {
3772 if (table->table[index] == 0)
3773 c += 1;
3774 else
3775 c = 0;
3776
3777 if (c == count) {
Joerg Roedel2b324502012-06-21 16:29:10 +02003778 for (; c != 0; --c)
3779 table->table[index - c + 1] = IRTE_ALLOCATED;
3780
3781 index -= count - 1;
Joerg Roedel2b324502012-06-21 16:29:10 +02003782 goto out;
3783 }
3784 }
3785
3786 index = -ENOSPC;
3787
3788out:
3789 spin_unlock_irqrestore(&table->lock, flags);
3790
3791 return index;
3792}
3793
Joerg Roedel2b324502012-06-21 16:29:10 +02003794static int modify_irte(u16 devid, int index, union irte irte)
3795{
3796 struct irq_remap_table *table;
3797 struct amd_iommu *iommu;
3798 unsigned long flags;
3799
3800 iommu = amd_iommu_rlookup_table[devid];
3801 if (iommu == NULL)
3802 return -EINVAL;
3803
3804 table = get_irq_table(devid, false);
3805 if (!table)
3806 return -ENOMEM;
3807
3808 spin_lock_irqsave(&table->lock, flags);
3809 table->table[index] = irte.val;
3810 spin_unlock_irqrestore(&table->lock, flags);
3811
3812 iommu_flush_irt(iommu, devid);
3813 iommu_completion_wait(iommu);
3814
3815 return 0;
3816}
3817
3818static void free_irte(u16 devid, int index)
3819{
3820 struct irq_remap_table *table;
3821 struct amd_iommu *iommu;
3822 unsigned long flags;
3823
3824 iommu = amd_iommu_rlookup_table[devid];
3825 if (iommu == NULL)
3826 return;
3827
3828 table = get_irq_table(devid, false);
3829 if (!table)
3830 return;
3831
3832 spin_lock_irqsave(&table->lock, flags);
3833 table->table[index] = 0;
3834 spin_unlock_irqrestore(&table->lock, flags);
3835
3836 iommu_flush_irt(iommu, devid);
3837 iommu_completion_wait(iommu);
3838}
3839
Jiang Liu7c71d302015-04-13 14:11:33 +08003840static int get_devid(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003841{
Jiang Liu7c71d302015-04-13 14:11:33 +08003842 int devid = -1;
Joerg Roedel5527de72012-06-26 11:17:32 +02003843
Jiang Liu7c71d302015-04-13 14:11:33 +08003844 switch (info->type) {
3845 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3846 devid = get_ioapic_devid(info->ioapic_id);
3847 break;
3848 case X86_IRQ_ALLOC_TYPE_HPET:
3849 devid = get_hpet_devid(info->hpet_id);
3850 break;
3851 case X86_IRQ_ALLOC_TYPE_MSI:
3852 case X86_IRQ_ALLOC_TYPE_MSIX:
3853 devid = get_device_id(&info->msi_dev->dev);
3854 break;
3855 default:
3856 BUG_ON(1);
3857 break;
Joerg Roedel5527de72012-06-26 11:17:32 +02003858 }
3859
Jiang Liu7c71d302015-04-13 14:11:33 +08003860 return devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003861}
3862
Jiang Liu7c71d302015-04-13 14:11:33 +08003863static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003864{
Jiang Liu7c71d302015-04-13 14:11:33 +08003865 struct amd_iommu *iommu;
3866 int devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003867
Jiang Liu7c71d302015-04-13 14:11:33 +08003868 if (!info)
3869 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003870
Jiang Liu7c71d302015-04-13 14:11:33 +08003871 devid = get_devid(info);
3872 if (devid >= 0) {
3873 iommu = amd_iommu_rlookup_table[devid];
3874 if (iommu)
3875 return iommu->ir_domain;
3876 }
Joerg Roedel5527de72012-06-26 11:17:32 +02003877
Jiang Liu7c71d302015-04-13 14:11:33 +08003878 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003879}
3880
Jiang Liu7c71d302015-04-13 14:11:33 +08003881static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003882{
Jiang Liu7c71d302015-04-13 14:11:33 +08003883 struct amd_iommu *iommu;
3884 int devid;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003885
Jiang Liu7c71d302015-04-13 14:11:33 +08003886 if (!info)
3887 return NULL;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003888
Jiang Liu7c71d302015-04-13 14:11:33 +08003889 switch (info->type) {
3890 case X86_IRQ_ALLOC_TYPE_MSI:
3891 case X86_IRQ_ALLOC_TYPE_MSIX:
3892 devid = get_device_id(&info->msi_dev->dev);
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003893 if (IS_ERR_VALUE(devid))
3894 return NULL;
3895
Dan Carpenter1fb260b2016-01-07 12:36:06 +03003896 iommu = amd_iommu_rlookup_table[devid];
3897 if (iommu)
3898 return iommu->msi_domain;
Jiang Liu7c71d302015-04-13 14:11:33 +08003899 break;
3900 default:
3901 break;
3902 }
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003903
Jiang Liu7c71d302015-04-13 14:11:33 +08003904 return NULL;
Joerg Roedeld9761952012-06-26 16:00:08 +02003905}
3906
Joerg Roedel6b474b82012-06-26 16:46:04 +02003907struct irq_remap_ops amd_iommu_irq_ops = {
Joerg Roedel6b474b82012-06-26 16:46:04 +02003908 .prepare = amd_iommu_prepare,
3909 .enable = amd_iommu_enable,
3910 .disable = amd_iommu_disable,
3911 .reenable = amd_iommu_reenable,
3912 .enable_faulting = amd_iommu_enable_faulting,
Jiang Liu7c71d302015-04-13 14:11:33 +08003913 .get_ir_irq_domain = get_ir_irq_domain,
3914 .get_irq_domain = get_irq_domain,
Joerg Roedel6b474b82012-06-26 16:46:04 +02003915};
Jiang Liu7c71d302015-04-13 14:11:33 +08003916
3917static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3918 struct irq_cfg *irq_cfg,
3919 struct irq_alloc_info *info,
3920 int devid, int index, int sub_handle)
3921{
3922 struct irq_2_irte *irte_info = &data->irq_2_irte;
3923 struct msi_msg *msg = &data->msi_entry;
3924 union irte *irte = &data->irte_entry;
3925 struct IO_APIC_route_entry *entry;
3926
Jiang Liu7c71d302015-04-13 14:11:33 +08003927 data->irq_2_irte.devid = devid;
3928 data->irq_2_irte.index = index + sub_handle;
3929
3930 /* Setup IRTE for IOMMU */
3931 irte->val = 0;
3932 irte->fields.vector = irq_cfg->vector;
3933 irte->fields.int_type = apic->irq_delivery_mode;
3934 irte->fields.destination = irq_cfg->dest_apicid;
3935 irte->fields.dm = apic->irq_dest_mode;
3936 irte->fields.valid = 1;
3937
3938 switch (info->type) {
3939 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3940 /* Setup IOAPIC entry */
3941 entry = info->ioapic_entry;
3942 info->ioapic_entry = NULL;
3943 memset(entry, 0, sizeof(*entry));
3944 entry->vector = index;
3945 entry->mask = 0;
3946 entry->trigger = info->ioapic_trigger;
3947 entry->polarity = info->ioapic_polarity;
3948 /* Mask level triggered irqs. */
3949 if (info->ioapic_trigger)
3950 entry->mask = 1;
3951 break;
3952
3953 case X86_IRQ_ALLOC_TYPE_HPET:
3954 case X86_IRQ_ALLOC_TYPE_MSI:
3955 case X86_IRQ_ALLOC_TYPE_MSIX:
3956 msg->address_hi = MSI_ADDR_BASE_HI;
3957 msg->address_lo = MSI_ADDR_BASE_LO;
3958 msg->data = irte_info->index;
3959 break;
3960
3961 default:
3962 BUG_ON(1);
3963 break;
3964 }
3965}
3966
3967static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3968 unsigned int nr_irqs, void *arg)
3969{
3970 struct irq_alloc_info *info = arg;
3971 struct irq_data *irq_data;
3972 struct amd_ir_data *data;
3973 struct irq_cfg *cfg;
3974 int i, ret, devid;
3975 int index = -1;
3976
3977 if (!info)
3978 return -EINVAL;
3979 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
3980 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
3981 return -EINVAL;
3982
3983 /*
3984 * With IRQ remapping enabled, don't need contiguous CPU vectors
3985 * to support multiple MSI interrupts.
3986 */
3987 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
3988 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
3989
3990 devid = get_devid(info);
3991 if (devid < 0)
3992 return -EINVAL;
3993
3994 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3995 if (ret < 0)
3996 return ret;
3997
Jiang Liu7c71d302015-04-13 14:11:33 +08003998 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3999 if (get_irq_table(devid, true))
4000 index = info->ioapic_pin;
4001 else
4002 ret = -ENOMEM;
4003 } else {
Jiang Liu3c3d4f92015-04-13 14:11:38 +08004004 index = alloc_irq_index(devid, nr_irqs);
Jiang Liu7c71d302015-04-13 14:11:33 +08004005 }
4006 if (index < 0) {
4007 pr_warn("Failed to allocate IRTE\n");
Jiang Liu7c71d302015-04-13 14:11:33 +08004008 goto out_free_parent;
4009 }
4010
4011 for (i = 0; i < nr_irqs; i++) {
4012 irq_data = irq_domain_get_irq_data(domain, virq + i);
4013 cfg = irqd_cfg(irq_data);
4014 if (!irq_data || !cfg) {
4015 ret = -EINVAL;
4016 goto out_free_data;
4017 }
4018
Joerg Roedela130e692015-08-13 11:07:25 +02004019 ret = -ENOMEM;
4020 data = kzalloc(sizeof(*data), GFP_KERNEL);
4021 if (!data)
4022 goto out_free_data;
4023
Jiang Liu7c71d302015-04-13 14:11:33 +08004024 irq_data->hwirq = (devid << 16) + i;
4025 irq_data->chip_data = data;
4026 irq_data->chip = &amd_ir_chip;
4027 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
4028 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
4029 }
Joerg Roedela130e692015-08-13 11:07:25 +02004030
Jiang Liu7c71d302015-04-13 14:11:33 +08004031 return 0;
4032
4033out_free_data:
4034 for (i--; i >= 0; i--) {
4035 irq_data = irq_domain_get_irq_data(domain, virq + i);
4036 if (irq_data)
4037 kfree(irq_data->chip_data);
4038 }
4039 for (i = 0; i < nr_irqs; i++)
4040 free_irte(devid, index + i);
4041out_free_parent:
4042 irq_domain_free_irqs_common(domain, virq, nr_irqs);
4043 return ret;
4044}
4045
4046static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
4047 unsigned int nr_irqs)
4048{
4049 struct irq_2_irte *irte_info;
4050 struct irq_data *irq_data;
4051 struct amd_ir_data *data;
4052 int i;
4053
4054 for (i = 0; i < nr_irqs; i++) {
4055 irq_data = irq_domain_get_irq_data(domain, virq + i);
4056 if (irq_data && irq_data->chip_data) {
4057 data = irq_data->chip_data;
4058 irte_info = &data->irq_2_irte;
4059 free_irte(irte_info->devid, irte_info->index);
4060 kfree(data);
4061 }
4062 }
4063 irq_domain_free_irqs_common(domain, virq, nr_irqs);
4064}
4065
4066static void irq_remapping_activate(struct irq_domain *domain,
4067 struct irq_data *irq_data)
4068{
4069 struct amd_ir_data *data = irq_data->chip_data;
4070 struct irq_2_irte *irte_info = &data->irq_2_irte;
4071
4072 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
4073}
4074
4075static void irq_remapping_deactivate(struct irq_domain *domain,
4076 struct irq_data *irq_data)
4077{
4078 struct amd_ir_data *data = irq_data->chip_data;
4079 struct irq_2_irte *irte_info = &data->irq_2_irte;
4080 union irte entry;
4081
4082 entry.val = 0;
4083 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
4084}
4085
4086static struct irq_domain_ops amd_ir_domain_ops = {
4087 .alloc = irq_remapping_alloc,
4088 .free = irq_remapping_free,
4089 .activate = irq_remapping_activate,
4090 .deactivate = irq_remapping_deactivate,
4091};
4092
4093static int amd_ir_set_affinity(struct irq_data *data,
4094 const struct cpumask *mask, bool force)
4095{
4096 struct amd_ir_data *ir_data = data->chip_data;
4097 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
4098 struct irq_cfg *cfg = irqd_cfg(data);
4099 struct irq_data *parent = data->parent_data;
4100 int ret;
4101
4102 ret = parent->chip->irq_set_affinity(parent, mask, force);
4103 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
4104 return ret;
4105
4106 /*
4107 * Atomically updates the IRTE with the new destination, vector
4108 * and flushes the interrupt entry cache.
4109 */
4110 ir_data->irte_entry.fields.vector = cfg->vector;
4111 ir_data->irte_entry.fields.destination = cfg->dest_apicid;
4112 modify_irte(irte_info->devid, irte_info->index, ir_data->irte_entry);
4113
4114 /*
4115 * After this point, all the interrupts will start arriving
4116 * at the new destination. So, time to cleanup the previous
4117 * vector allocation.
4118 */
Jiang Liuc6c20022015-04-14 10:30:02 +08004119 send_cleanup_vector(cfg);
Jiang Liu7c71d302015-04-13 14:11:33 +08004120
4121 return IRQ_SET_MASK_OK_DONE;
4122}
4123
4124static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
4125{
4126 struct amd_ir_data *ir_data = irq_data->chip_data;
4127
4128 *msg = ir_data->msi_entry;
4129}
4130
4131static struct irq_chip amd_ir_chip = {
4132 .irq_ack = ir_ack_apic_edge,
4133 .irq_set_affinity = amd_ir_set_affinity,
4134 .irq_compose_msi_msg = ir_compose_msi_msg,
4135};
4136
4137int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
4138{
4139 iommu->ir_domain = irq_domain_add_tree(NULL, &amd_ir_domain_ops, iommu);
4140 if (!iommu->ir_domain)
4141 return -ENOMEM;
4142
4143 iommu->ir_domain->parent = arch_get_ir_parent_domain();
4144 iommu->msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
4145
4146 return 0;
4147}
Joerg Roedel2b324502012-06-21 16:29:10 +02004148#endif