amd_iommu.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572
  1. /*
  2. * Copyright (C) 2007-2009 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <joerg.roedel@amd.com>
  4. * 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. #include <linux/pci.h>
  20. #include <linux/gfp.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/scatterlist.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/iommu-helper.h>
  26. #include <linux/iommu.h>
  27. #include <asm/proto.h>
  28. #include <asm/iommu.h>
  29. #include <asm/gart.h>
  30. #include <asm/amd_iommu_proto.h>
  31. #include <asm/amd_iommu_types.h>
  32. #include <asm/amd_iommu.h>
  33. #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
  34. #define EXIT_LOOP_COUNT 10000000
  35. static DEFINE_RWLOCK(amd_iommu_devtable_lock);
  36. /* A list of preallocated protection domains */
  37. static LIST_HEAD(iommu_pd_list);
  38. static DEFINE_SPINLOCK(iommu_pd_list_lock);
  39. /*
  40. * Domain for untranslated devices - only allocated
  41. * if iommu=pt passed on kernel cmd line.
  42. */
  43. static struct protection_domain *pt_domain;
  44. static struct iommu_ops amd_iommu_ops;
  45. /*
  46. * general struct to manage commands send to an IOMMU
  47. */
  48. struct iommu_cmd {
  49. u32 data[4];
  50. };
  51. static void reset_iommu_command_buffer(struct amd_iommu *iommu);
  52. static void update_domain(struct protection_domain *domain);
  53. /****************************************************************************
  54. *
  55. * Helper functions
  56. *
  57. ****************************************************************************/
  58. static inline u16 get_device_id(struct device *dev)
  59. {
  60. struct pci_dev *pdev = to_pci_dev(dev);
  61. return calc_devid(pdev->bus->number, pdev->devfn);
  62. }
  63. static struct iommu_dev_data *get_dev_data(struct device *dev)
  64. {
  65. return dev->archdata.iommu;
  66. }
  67. /*
  68. * In this function the list of preallocated protection domains is traversed to
  69. * find the domain for a specific device
  70. */
  71. static struct dma_ops_domain *find_protection_domain(u16 devid)
  72. {
  73. struct dma_ops_domain *entry, *ret = NULL;
  74. unsigned long flags;
  75. u16 alias = amd_iommu_alias_table[devid];
  76. if (list_empty(&iommu_pd_list))
  77. return NULL;
  78. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  79. list_for_each_entry(entry, &iommu_pd_list, list) {
  80. if (entry->target_dev == devid ||
  81. entry->target_dev == alias) {
  82. ret = entry;
  83. break;
  84. }
  85. }
  86. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  87. return ret;
  88. }
  89. /*
  90. * This function checks if the driver got a valid device from the caller to
  91. * avoid dereferencing invalid pointers.
  92. */
  93. static bool check_device(struct device *dev)
  94. {
  95. u16 devid;
  96. if (!dev || !dev->dma_mask)
  97. return false;
  98. /* No device or no PCI device */
  99. if (dev->bus != &pci_bus_type)
  100. return false;
  101. devid = get_device_id(dev);
  102. /* Out of our scope? */
  103. if (devid > amd_iommu_last_bdf)
  104. return false;
  105. if (amd_iommu_rlookup_table[devid] == NULL)
  106. return false;
  107. return true;
  108. }
  109. static int iommu_init_device(struct device *dev)
  110. {
  111. struct iommu_dev_data *dev_data;
  112. struct pci_dev *pdev;
  113. u16 devid, alias;
  114. if (dev->archdata.iommu)
  115. return 0;
  116. dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
  117. if (!dev_data)
  118. return -ENOMEM;
  119. dev_data->dev = dev;
  120. devid = get_device_id(dev);
  121. alias = amd_iommu_alias_table[devid];
  122. pdev = pci_get_bus_and_slot(PCI_BUS(alias), alias & 0xff);
  123. if (pdev)
  124. dev_data->alias = &pdev->dev;
  125. atomic_set(&dev_data->bind, 0);
  126. dev->archdata.iommu = dev_data;
  127. return 0;
  128. }
  129. static void iommu_uninit_device(struct device *dev)
  130. {
  131. kfree(dev->archdata.iommu);
  132. }
  133. void __init amd_iommu_uninit_devices(void)
  134. {
  135. struct pci_dev *pdev = NULL;
  136. for_each_pci_dev(pdev) {
  137. if (!check_device(&pdev->dev))
  138. continue;
  139. iommu_uninit_device(&pdev->dev);
  140. }
  141. }
  142. int __init amd_iommu_init_devices(void)
  143. {
  144. struct pci_dev *pdev = NULL;
  145. int ret = 0;
  146. for_each_pci_dev(pdev) {
  147. if (!check_device(&pdev->dev))
  148. continue;
  149. ret = iommu_init_device(&pdev->dev);
  150. if (ret)
  151. goto out_free;
  152. }
  153. return 0;
  154. out_free:
  155. amd_iommu_uninit_devices();
  156. return ret;
  157. }
  158. #ifdef CONFIG_AMD_IOMMU_STATS
  159. /*
  160. * Initialization code for statistics collection
  161. */
  162. DECLARE_STATS_COUNTER(compl_wait);
  163. DECLARE_STATS_COUNTER(cnt_map_single);
  164. DECLARE_STATS_COUNTER(cnt_unmap_single);
  165. DECLARE_STATS_COUNTER(cnt_map_sg);
  166. DECLARE_STATS_COUNTER(cnt_unmap_sg);
  167. DECLARE_STATS_COUNTER(cnt_alloc_coherent);
  168. DECLARE_STATS_COUNTER(cnt_free_coherent);
  169. DECLARE_STATS_COUNTER(cross_page);
  170. DECLARE_STATS_COUNTER(domain_flush_single);
  171. DECLARE_STATS_COUNTER(domain_flush_all);
  172. DECLARE_STATS_COUNTER(alloced_io_mem);
  173. DECLARE_STATS_COUNTER(total_map_requests);
  174. static struct dentry *stats_dir;
  175. static struct dentry *de_fflush;
  176. static void amd_iommu_stats_add(struct __iommu_counter *cnt)
  177. {
  178. if (stats_dir == NULL)
  179. return;
  180. cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
  181. &cnt->value);
  182. }
  183. static void amd_iommu_stats_init(void)
  184. {
  185. stats_dir = debugfs_create_dir("amd-iommu", NULL);
  186. if (stats_dir == NULL)
  187. return;
  188. de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
  189. (u32 *)&amd_iommu_unmap_flush);
  190. amd_iommu_stats_add(&compl_wait);
  191. amd_iommu_stats_add(&cnt_map_single);
  192. amd_iommu_stats_add(&cnt_unmap_single);
  193. amd_iommu_stats_add(&cnt_map_sg);
  194. amd_iommu_stats_add(&cnt_unmap_sg);
  195. amd_iommu_stats_add(&cnt_alloc_coherent);
  196. amd_iommu_stats_add(&cnt_free_coherent);
  197. amd_iommu_stats_add(&cross_page);
  198. amd_iommu_stats_add(&domain_flush_single);
  199. amd_iommu_stats_add(&domain_flush_all);
  200. amd_iommu_stats_add(&alloced_io_mem);
  201. amd_iommu_stats_add(&total_map_requests);
  202. }
  203. #endif
  204. /****************************************************************************
  205. *
  206. * Interrupt handling functions
  207. *
  208. ****************************************************************************/
  209. static void dump_dte_entry(u16 devid)
  210. {
  211. int i;
  212. for (i = 0; i < 8; ++i)
  213. pr_err("AMD-Vi: DTE[%d]: %08x\n", i,
  214. amd_iommu_dev_table[devid].data[i]);
  215. }
  216. static void dump_command(unsigned long phys_addr)
  217. {
  218. struct iommu_cmd *cmd = phys_to_virt(phys_addr);
  219. int i;
  220. for (i = 0; i < 4; ++i)
  221. pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
  222. }
  223. static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
  224. {
  225. u32 *event = __evt;
  226. int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  227. int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  228. int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
  229. int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  230. u64 address = (u64)(((u64)event[3]) << 32) | event[2];
  231. printk(KERN_ERR "AMD-Vi: Event logged [");
  232. switch (type) {
  233. case EVENT_TYPE_ILL_DEV:
  234. printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
  235. "address=0x%016llx flags=0x%04x]\n",
  236. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  237. address, flags);
  238. dump_dte_entry(devid);
  239. break;
  240. case EVENT_TYPE_IO_FAULT:
  241. printk("IO_PAGE_FAULT device=%02x:%02x.%x "
  242. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  243. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  244. domid, address, flags);
  245. break;
  246. case EVENT_TYPE_DEV_TAB_ERR:
  247. printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  248. "address=0x%016llx flags=0x%04x]\n",
  249. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  250. address, flags);
  251. break;
  252. case EVENT_TYPE_PAGE_TAB_ERR:
  253. printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  254. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  255. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  256. domid, address, flags);
  257. break;
  258. case EVENT_TYPE_ILL_CMD:
  259. printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
  260. iommu->reset_in_progress = true;
  261. reset_iommu_command_buffer(iommu);
  262. dump_command(address);
  263. break;
  264. case EVENT_TYPE_CMD_HARD_ERR:
  265. printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
  266. "flags=0x%04x]\n", address, flags);
  267. break;
  268. case EVENT_TYPE_IOTLB_INV_TO:
  269. printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
  270. "address=0x%016llx]\n",
  271. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  272. address);
  273. break;
  274. case EVENT_TYPE_INV_DEV_REQ:
  275. printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
  276. "address=0x%016llx flags=0x%04x]\n",
  277. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  278. address, flags);
  279. break;
  280. default:
  281. printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
  282. }
  283. }
  284. static void iommu_poll_events(struct amd_iommu *iommu)
  285. {
  286. u32 head, tail;
  287. unsigned long flags;
  288. spin_lock_irqsave(&iommu->lock, flags);
  289. head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  290. tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  291. while (head != tail) {
  292. iommu_print_event(iommu, iommu->evt_buf + head);
  293. head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
  294. }
  295. writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  296. spin_unlock_irqrestore(&iommu->lock, flags);
  297. }
  298. irqreturn_t amd_iommu_int_handler(int irq, void *data)
  299. {
  300. struct amd_iommu *iommu;
  301. for_each_iommu(iommu)
  302. iommu_poll_events(iommu);
  303. return IRQ_HANDLED;
  304. }
  305. /****************************************************************************
  306. *
  307. * IOMMU command queuing functions
  308. *
  309. ****************************************************************************/
  310. /*
  311. * Writes the command to the IOMMUs command buffer and informs the
  312. * hardware about the new command. Must be called with iommu->lock held.
  313. */
  314. static int __iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  315. {
  316. u32 tail, head;
  317. u8 *target;
  318. WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
  319. tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  320. target = iommu->cmd_buf + tail;
  321. memcpy_toio(target, cmd, sizeof(*cmd));
  322. tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
  323. head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  324. if (tail == head)
  325. return -ENOMEM;
  326. writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  327. return 0;
  328. }
  329. /*
  330. * General queuing function for commands. Takes iommu->lock and calls
  331. * __iommu_queue_command().
  332. */
  333. static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  334. {
  335. unsigned long flags;
  336. int ret;
  337. spin_lock_irqsave(&iommu->lock, flags);
  338. ret = __iommu_queue_command(iommu, cmd);
  339. if (!ret)
  340. iommu->need_sync = true;
  341. spin_unlock_irqrestore(&iommu->lock, flags);
  342. return ret;
  343. }
  344. /*
  345. * This function waits until an IOMMU has completed a completion
  346. * wait command
  347. */
  348. static void __iommu_wait_for_completion(struct amd_iommu *iommu)
  349. {
  350. int ready = 0;
  351. unsigned status = 0;
  352. unsigned long i = 0;
  353. INC_STATS_COUNTER(compl_wait);
  354. while (!ready && (i < EXIT_LOOP_COUNT)) {
  355. ++i;
  356. /* wait for the bit to become one */
  357. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  358. ready = status & MMIO_STATUS_COM_WAIT_INT_MASK;
  359. }
  360. /* set bit back to zero */
  361. status &= ~MMIO_STATUS_COM_WAIT_INT_MASK;
  362. writel(status, iommu->mmio_base + MMIO_STATUS_OFFSET);
  363. if (unlikely(i == EXIT_LOOP_COUNT))
  364. iommu->reset_in_progress = true;
  365. }
  366. /*
  367. * This function queues a completion wait command into the command
  368. * buffer of an IOMMU
  369. */
  370. static int __iommu_completion_wait(struct amd_iommu *iommu)
  371. {
  372. struct iommu_cmd cmd;
  373. memset(&cmd, 0, sizeof(cmd));
  374. cmd.data[0] = CMD_COMPL_WAIT_INT_MASK;
  375. CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT);
  376. return __iommu_queue_command(iommu, &cmd);
  377. }
  378. /*
  379. * This function is called whenever we need to ensure that the IOMMU has
  380. * completed execution of all commands we sent. It sends a
  381. * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs
  382. * us about that by writing a value to a physical address we pass with
  383. * the command.
  384. */
  385. static int iommu_completion_wait(struct amd_iommu *iommu)
  386. {
  387. int ret = 0;
  388. unsigned long flags;
  389. spin_lock_irqsave(&iommu->lock, flags);
  390. if (!iommu->need_sync)
  391. goto out;
  392. ret = __iommu_completion_wait(iommu);
  393. iommu->need_sync = false;
  394. if (ret)
  395. goto out;
  396. __iommu_wait_for_completion(iommu);
  397. out:
  398. spin_unlock_irqrestore(&iommu->lock, flags);
  399. if (iommu->reset_in_progress)
  400. reset_iommu_command_buffer(iommu);
  401. return 0;
  402. }
  403. static void iommu_flush_complete(struct protection_domain *domain)
  404. {
  405. int i;
  406. for (i = 0; i < amd_iommus_present; ++i) {
  407. if (!domain->dev_iommu[i])
  408. continue;
  409. /*
  410. * Devices of this domain are behind this IOMMU
  411. * We need to wait for completion of all commands.
  412. */
  413. iommu_completion_wait(amd_iommus[i]);
  414. }
  415. }
  416. /*
  417. * Command send function for invalidating a device table entry
  418. */
  419. static int iommu_flush_device(struct device *dev)
  420. {
  421. struct amd_iommu *iommu;
  422. struct iommu_cmd cmd;
  423. u16 devid;
  424. devid = get_device_id(dev);
  425. iommu = amd_iommu_rlookup_table[devid];
  426. /* Build command */
  427. memset(&cmd, 0, sizeof(cmd));
  428. CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY);
  429. cmd.data[0] = devid;
  430. return iommu_queue_command(iommu, &cmd);
  431. }
  432. static void __iommu_build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
  433. u16 domid, int pde, int s)
  434. {
  435. memset(cmd, 0, sizeof(*cmd));
  436. address &= PAGE_MASK;
  437. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  438. cmd->data[1] |= domid;
  439. cmd->data[2] = lower_32_bits(address);
  440. cmd->data[3] = upper_32_bits(address);
  441. if (s) /* size bit - we flush more than one 4kb page */
  442. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  443. if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
  444. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  445. }
  446. /*
  447. * Generic command send function for invalidaing TLB entries
  448. */
  449. static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu,
  450. u64 address, u16 domid, int pde, int s)
  451. {
  452. struct iommu_cmd cmd;
  453. int ret;
  454. __iommu_build_inv_iommu_pages(&cmd, address, domid, pde, s);
  455. ret = iommu_queue_command(iommu, &cmd);
  456. return ret;
  457. }
  458. /*
  459. * TLB invalidation function which is called from the mapping functions.
  460. * It invalidates a single PTE if the range to flush is within a single
  461. * page. Otherwise it flushes the whole TLB of the IOMMU.
  462. */
  463. static void __iommu_flush_pages(struct protection_domain *domain,
  464. u64 address, size_t size, int pde)
  465. {
  466. int s = 0, i;
  467. unsigned long pages = iommu_num_pages(address, size, PAGE_SIZE);
  468. address &= PAGE_MASK;
  469. if (pages > 1) {
  470. /*
  471. * If we have to flush more than one page, flush all
  472. * TLB entries for this domain
  473. */
  474. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  475. s = 1;
  476. }
  477. for (i = 0; i < amd_iommus_present; ++i) {
  478. if (!domain->dev_iommu[i])
  479. continue;
  480. /*
  481. * Devices of this domain are behind this IOMMU
  482. * We need a TLB flush
  483. */
  484. iommu_queue_inv_iommu_pages(amd_iommus[i], address,
  485. domain->id, pde, s);
  486. }
  487. return;
  488. }
  489. static void iommu_flush_pages(struct protection_domain *domain,
  490. u64 address, size_t size)
  491. {
  492. __iommu_flush_pages(domain, address, size, 0);
  493. }
  494. /* Flush the whole IO/TLB for a given protection domain */
  495. static void iommu_flush_tlb(struct protection_domain *domain)
  496. {
  497. __iommu_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
  498. }
  499. /* Flush the whole IO/TLB for a given protection domain - including PDE */
  500. static void iommu_flush_tlb_pde(struct protection_domain *domain)
  501. {
  502. __iommu_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
  503. }
  504. /*
  505. * This function flushes the DTEs for all devices in domain
  506. */
  507. static void iommu_flush_domain_devices(struct protection_domain *domain)
  508. {
  509. struct iommu_dev_data *dev_data;
  510. unsigned long flags;
  511. spin_lock_irqsave(&domain->lock, flags);
  512. list_for_each_entry(dev_data, &domain->dev_list, list)
  513. iommu_flush_device(dev_data->dev);
  514. spin_unlock_irqrestore(&domain->lock, flags);
  515. }
  516. static void iommu_flush_all_domain_devices(void)
  517. {
  518. struct protection_domain *domain;
  519. unsigned long flags;
  520. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  521. list_for_each_entry(domain, &amd_iommu_pd_list, list) {
  522. iommu_flush_domain_devices(domain);
  523. iommu_flush_complete(domain);
  524. }
  525. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  526. }
  527. void amd_iommu_flush_all_devices(void)
  528. {
  529. iommu_flush_all_domain_devices();
  530. }
  531. /*
  532. * This function uses heavy locking and may disable irqs for some time. But
  533. * this is no issue because it is only called during resume.
  534. */
  535. void amd_iommu_flush_all_domains(void)
  536. {
  537. struct protection_domain *domain;
  538. unsigned long flags;
  539. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  540. list_for_each_entry(domain, &amd_iommu_pd_list, list) {
  541. spin_lock(&domain->lock);
  542. iommu_flush_tlb_pde(domain);
  543. iommu_flush_complete(domain);
  544. spin_unlock(&domain->lock);
  545. }
  546. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  547. }
  548. static void reset_iommu_command_buffer(struct amd_iommu *iommu)
  549. {
  550. pr_err("AMD-Vi: Resetting IOMMU command buffer\n");
  551. if (iommu->reset_in_progress)
  552. panic("AMD-Vi: ILLEGAL_COMMAND_ERROR while resetting command buffer\n");
  553. amd_iommu_reset_cmd_buffer(iommu);
  554. amd_iommu_flush_all_devices();
  555. amd_iommu_flush_all_domains();
  556. iommu->reset_in_progress = false;
  557. }
  558. /****************************************************************************
  559. *
  560. * The functions below are used the create the page table mappings for
  561. * unity mapped regions.
  562. *
  563. ****************************************************************************/
  564. /*
  565. * This function is used to add another level to an IO page table. Adding
  566. * another level increases the size of the address space by 9 bits to a size up
  567. * to 64 bits.
  568. */
  569. static bool increase_address_space(struct protection_domain *domain,
  570. gfp_t gfp)
  571. {
  572. u64 *pte;
  573. if (domain->mode == PAGE_MODE_6_LEVEL)
  574. /* address space already 64 bit large */
  575. return false;
  576. pte = (void *)get_zeroed_page(gfp);
  577. if (!pte)
  578. return false;
  579. *pte = PM_LEVEL_PDE(domain->mode,
  580. virt_to_phys(domain->pt_root));
  581. domain->pt_root = pte;
  582. domain->mode += 1;
  583. domain->updated = true;
  584. return true;
  585. }
  586. static u64 *alloc_pte(struct protection_domain *domain,
  587. unsigned long address,
  588. int end_lvl,
  589. u64 **pte_page,
  590. gfp_t gfp)
  591. {
  592. u64 *pte, *page;
  593. int level;
  594. while (address > PM_LEVEL_SIZE(domain->mode))
  595. increase_address_space(domain, gfp);
  596. level = domain->mode - 1;
  597. pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
  598. while (level > end_lvl) {
  599. if (!IOMMU_PTE_PRESENT(*pte)) {
  600. page = (u64 *)get_zeroed_page(gfp);
  601. if (!page)
  602. return NULL;
  603. *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
  604. }
  605. level -= 1;
  606. pte = IOMMU_PTE_PAGE(*pte);
  607. if (pte_page && level == end_lvl)
  608. *pte_page = pte;
  609. pte = &pte[PM_LEVEL_INDEX(level, address)];
  610. }
  611. return pte;
  612. }
  613. /*
  614. * This function checks if there is a PTE for a given dma address. If
  615. * there is one, it returns the pointer to it.
  616. */
  617. static u64 *fetch_pte(struct protection_domain *domain,
  618. unsigned long address, int map_size)
  619. {
  620. int level;
  621. u64 *pte;
  622. level = domain->mode - 1;
  623. pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
  624. while (level > map_size) {
  625. if (!IOMMU_PTE_PRESENT(*pte))
  626. return NULL;
  627. level -= 1;
  628. pte = IOMMU_PTE_PAGE(*pte);
  629. pte = &pte[PM_LEVEL_INDEX(level, address)];
  630. if ((PM_PTE_LEVEL(*pte) == 0) && level != map_size) {
  631. pte = NULL;
  632. break;
  633. }
  634. }
  635. return pte;
  636. }
  637. /*
  638. * Generic mapping functions. It maps a physical address into a DMA
  639. * address space. It allocates the page table pages if necessary.
  640. * In the future it can be extended to a generic mapping function
  641. * supporting all features of AMD IOMMU page tables like level skipping
  642. * and full 64 bit address spaces.
  643. */
  644. static int iommu_map_page(struct protection_domain *dom,
  645. unsigned long bus_addr,
  646. unsigned long phys_addr,
  647. int prot,
  648. int map_size)
  649. {
  650. u64 __pte, *pte;
  651. bus_addr = PAGE_ALIGN(bus_addr);
  652. phys_addr = PAGE_ALIGN(phys_addr);
  653. BUG_ON(!PM_ALIGNED(map_size, bus_addr));
  654. BUG_ON(!PM_ALIGNED(map_size, phys_addr));
  655. if (!(prot & IOMMU_PROT_MASK))
  656. return -EINVAL;
  657. pte = alloc_pte(dom, bus_addr, map_size, NULL, GFP_KERNEL);
  658. if (IOMMU_PTE_PRESENT(*pte))
  659. return -EBUSY;
  660. __pte = phys_addr | IOMMU_PTE_P;
  661. if (prot & IOMMU_PROT_IR)
  662. __pte |= IOMMU_PTE_IR;
  663. if (prot & IOMMU_PROT_IW)
  664. __pte |= IOMMU_PTE_IW;
  665. *pte = __pte;
  666. update_domain(dom);
  667. return 0;
  668. }
  669. static void iommu_unmap_page(struct protection_domain *dom,
  670. unsigned long bus_addr, int map_size)
  671. {
  672. u64 *pte = fetch_pte(dom, bus_addr, map_size);
  673. if (pte)
  674. *pte = 0;
  675. }
  676. /*
  677. * This function checks if a specific unity mapping entry is needed for
  678. * this specific IOMMU.
  679. */
  680. static int iommu_for_unity_map(struct amd_iommu *iommu,
  681. struct unity_map_entry *entry)
  682. {
  683. u16 bdf, i;
  684. for (i = entry->devid_start; i <= entry->devid_end; ++i) {
  685. bdf = amd_iommu_alias_table[i];
  686. if (amd_iommu_rlookup_table[bdf] == iommu)
  687. return 1;
  688. }
  689. return 0;
  690. }
  691. /*
  692. * This function actually applies the mapping to the page table of the
  693. * dma_ops domain.
  694. */
  695. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  696. struct unity_map_entry *e)
  697. {
  698. u64 addr;
  699. int ret;
  700. for (addr = e->address_start; addr < e->address_end;
  701. addr += PAGE_SIZE) {
  702. ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
  703. PM_MAP_4k);
  704. if (ret)
  705. return ret;
  706. /*
  707. * if unity mapping is in aperture range mark the page
  708. * as allocated in the aperture
  709. */
  710. if (addr < dma_dom->aperture_size)
  711. __set_bit(addr >> PAGE_SHIFT,
  712. dma_dom->aperture[0]->bitmap);
  713. }
  714. return 0;
  715. }
  716. /*
  717. * Init the unity mappings for a specific IOMMU in the system
  718. *
  719. * Basically iterates over all unity mapping entries and applies them to
  720. * the default domain DMA of that IOMMU if necessary.
  721. */
  722. static int iommu_init_unity_mappings(struct amd_iommu *iommu)
  723. {
  724. struct unity_map_entry *entry;
  725. int ret;
  726. list_for_each_entry(entry, &amd_iommu_unity_map, list) {
  727. if (!iommu_for_unity_map(iommu, entry))
  728. continue;
  729. ret = dma_ops_unity_map(iommu->default_dom, entry);
  730. if (ret)
  731. return ret;
  732. }
  733. return 0;
  734. }
  735. /*
  736. * Inits the unity mappings required for a specific device
  737. */
  738. static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
  739. u16 devid)
  740. {
  741. struct unity_map_entry *e;
  742. int ret;
  743. list_for_each_entry(e, &amd_iommu_unity_map, list) {
  744. if (!(devid >= e->devid_start && devid <= e->devid_end))
  745. continue;
  746. ret = dma_ops_unity_map(dma_dom, e);
  747. if (ret)
  748. return ret;
  749. }
  750. return 0;
  751. }
  752. /****************************************************************************
  753. *
  754. * The next functions belong to the address allocator for the dma_ops
  755. * interface functions. They work like the allocators in the other IOMMU
  756. * drivers. Its basically a bitmap which marks the allocated pages in
  757. * the aperture. Maybe it could be enhanced in the future to a more
  758. * efficient allocator.
  759. *
  760. ****************************************************************************/
  761. /*
  762. * The address allocator core functions.
  763. *
  764. * called with domain->lock held
  765. */
  766. /*
  767. * Used to reserve address ranges in the aperture (e.g. for exclusion
  768. * ranges.
  769. */
  770. static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
  771. unsigned long start_page,
  772. unsigned int pages)
  773. {
  774. unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
  775. if (start_page + pages > last_page)
  776. pages = last_page - start_page;
  777. for (i = start_page; i < start_page + pages; ++i) {
  778. int index = i / APERTURE_RANGE_PAGES;
  779. int page = i % APERTURE_RANGE_PAGES;
  780. __set_bit(page, dom->aperture[index]->bitmap);
  781. }
  782. }
  783. /*
  784. * This function is used to add a new aperture range to an existing
  785. * aperture in case of dma_ops domain allocation or address allocation
  786. * failure.
  787. */
  788. static int alloc_new_range(struct dma_ops_domain *dma_dom,
  789. bool populate, gfp_t gfp)
  790. {
  791. int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
  792. struct amd_iommu *iommu;
  793. unsigned long i;
  794. #ifdef CONFIG_IOMMU_STRESS
  795. populate = false;
  796. #endif
  797. if (index >= APERTURE_MAX_RANGES)
  798. return -ENOMEM;
  799. dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
  800. if (!dma_dom->aperture[index])
  801. return -ENOMEM;
  802. dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
  803. if (!dma_dom->aperture[index]->bitmap)
  804. goto out_free;
  805. dma_dom->aperture[index]->offset = dma_dom->aperture_size;
  806. if (populate) {
  807. unsigned long address = dma_dom->aperture_size;
  808. int i, num_ptes = APERTURE_RANGE_PAGES / 512;
  809. u64 *pte, *pte_page;
  810. for (i = 0; i < num_ptes; ++i) {
  811. pte = alloc_pte(&dma_dom->domain, address, PM_MAP_4k,
  812. &pte_page, gfp);
  813. if (!pte)
  814. goto out_free;
  815. dma_dom->aperture[index]->pte_pages[i] = pte_page;
  816. address += APERTURE_RANGE_SIZE / 64;
  817. }
  818. }
  819. dma_dom->aperture_size += APERTURE_RANGE_SIZE;
  820. /* Intialize the exclusion range if necessary */
  821. for_each_iommu(iommu) {
  822. if (iommu->exclusion_start &&
  823. iommu->exclusion_start >= dma_dom->aperture[index]->offset
  824. && iommu->exclusion_start < dma_dom->aperture_size) {
  825. unsigned long startpage;
  826. int pages = iommu_num_pages(iommu->exclusion_start,
  827. iommu->exclusion_length,
  828. PAGE_SIZE);
  829. startpage = iommu->exclusion_start >> PAGE_SHIFT;
  830. dma_ops_reserve_addresses(dma_dom, startpage, pages);
  831. }
  832. }
  833. /*
  834. * Check for areas already mapped as present in the new aperture
  835. * range and mark those pages as reserved in the allocator. Such
  836. * mappings may already exist as a result of requested unity
  837. * mappings for devices.
  838. */
  839. for (i = dma_dom->aperture[index]->offset;
  840. i < dma_dom->aperture_size;
  841. i += PAGE_SIZE) {
  842. u64 *pte = fetch_pte(&dma_dom->domain, i, PM_MAP_4k);
  843. if (!pte || !IOMMU_PTE_PRESENT(*pte))
  844. continue;
  845. dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
  846. }
  847. update_domain(&dma_dom->domain);
  848. return 0;
  849. out_free:
  850. update_domain(&dma_dom->domain);
  851. free_page((unsigned long)dma_dom->aperture[index]->bitmap);
  852. kfree(dma_dom->aperture[index]);
  853. dma_dom->aperture[index] = NULL;
  854. return -ENOMEM;
  855. }
  856. static unsigned long dma_ops_area_alloc(struct device *dev,
  857. struct dma_ops_domain *dom,
  858. unsigned int pages,
  859. unsigned long align_mask,
  860. u64 dma_mask,
  861. unsigned long start)
  862. {
  863. unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
  864. int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
  865. int i = start >> APERTURE_RANGE_SHIFT;
  866. unsigned long boundary_size;
  867. unsigned long address = -1;
  868. unsigned long limit;
  869. next_bit >>= PAGE_SHIFT;
  870. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  871. PAGE_SIZE) >> PAGE_SHIFT;
  872. for (;i < max_index; ++i) {
  873. unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
  874. if (dom->aperture[i]->offset >= dma_mask)
  875. break;
  876. limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
  877. dma_mask >> PAGE_SHIFT);
  878. address = iommu_area_alloc(dom->aperture[i]->bitmap,
  879. limit, next_bit, pages, 0,
  880. boundary_size, align_mask);
  881. if (address != -1) {
  882. address = dom->aperture[i]->offset +
  883. (address << PAGE_SHIFT);
  884. dom->next_address = address + (pages << PAGE_SHIFT);
  885. break;
  886. }
  887. next_bit = 0;
  888. }
  889. return address;
  890. }
  891. static unsigned long dma_ops_alloc_addresses(struct device *dev,
  892. struct dma_ops_domain *dom,
  893. unsigned int pages,
  894. unsigned long align_mask,
  895. u64 dma_mask)
  896. {
  897. unsigned long address;
  898. #ifdef CONFIG_IOMMU_STRESS
  899. dom->next_address = 0;
  900. dom->need_flush = true;
  901. #endif
  902. address = dma_ops_area_alloc(dev, dom, pages, align_mask,
  903. dma_mask, dom->next_address);
  904. if (address == -1) {
  905. dom->next_address = 0;
  906. address = dma_ops_area_alloc(dev, dom, pages, align_mask,
  907. dma_mask, 0);
  908. dom->need_flush = true;
  909. }
  910. if (unlikely(address == -1))
  911. address = DMA_ERROR_CODE;
  912. WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
  913. return address;
  914. }
  915. /*
  916. * The address free function.
  917. *
  918. * called with domain->lock held
  919. */
  920. static void dma_ops_free_addresses(struct dma_ops_domain *dom,
  921. unsigned long address,
  922. unsigned int pages)
  923. {
  924. unsigned i = address >> APERTURE_RANGE_SHIFT;
  925. struct aperture_range *range = dom->aperture[i];
  926. BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
  927. #ifdef CONFIG_IOMMU_STRESS
  928. if (i < 4)
  929. return;
  930. #endif
  931. if (address >= dom->next_address)
  932. dom->need_flush = true;
  933. address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
  934. bitmap_clear(range->bitmap, address, pages);
  935. }
  936. /****************************************************************************
  937. *
  938. * The next functions belong to the domain allocation. A domain is
  939. * allocated for every IOMMU as the default domain. If device isolation
  940. * is enabled, every device get its own domain. The most important thing
  941. * about domains is the page table mapping the DMA address space they
  942. * contain.
  943. *
  944. ****************************************************************************/
  945. /*
  946. * This function adds a protection domain to the global protection domain list
  947. */
  948. static void add_domain_to_list(struct protection_domain *domain)
  949. {
  950. unsigned long flags;
  951. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  952. list_add(&domain->list, &amd_iommu_pd_list);
  953. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  954. }
  955. /*
  956. * This function removes a protection domain to the global
  957. * protection domain list
  958. */
  959. static void del_domain_from_list(struct protection_domain *domain)
  960. {
  961. unsigned long flags;
  962. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  963. list_del(&domain->list);
  964. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  965. }
  966. static u16 domain_id_alloc(void)
  967. {
  968. unsigned long flags;
  969. int id;
  970. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  971. id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
  972. BUG_ON(id == 0);
  973. if (id > 0 && id < MAX_DOMAIN_ID)
  974. __set_bit(id, amd_iommu_pd_alloc_bitmap);
  975. else
  976. id = 0;
  977. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  978. return id;
  979. }
  980. static void domain_id_free(int id)
  981. {
  982. unsigned long flags;
  983. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  984. if (id > 0 && id < MAX_DOMAIN_ID)
  985. __clear_bit(id, amd_iommu_pd_alloc_bitmap);
  986. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  987. }
  988. static void free_pagetable(struct protection_domain *domain)
  989. {
  990. int i, j;
  991. u64 *p1, *p2, *p3;
  992. p1 = domain->pt_root;
  993. if (!p1)
  994. return;
  995. for (i = 0; i < 512; ++i) {
  996. if (!IOMMU_PTE_PRESENT(p1[i]))
  997. continue;
  998. p2 = IOMMU_PTE_PAGE(p1[i]);
  999. for (j = 0; j < 512; ++j) {
  1000. if (!IOMMU_PTE_PRESENT(p2[j]))
  1001. continue;
  1002. p3 = IOMMU_PTE_PAGE(p2[j]);
  1003. free_page((unsigned long)p3);
  1004. }
  1005. free_page((unsigned long)p2);
  1006. }
  1007. free_page((unsigned long)p1);
  1008. domain->pt_root = NULL;
  1009. }
  1010. /*
  1011. * Free a domain, only used if something went wrong in the
  1012. * allocation path and we need to free an already allocated page table
  1013. */
  1014. static void dma_ops_domain_free(struct dma_ops_domain *dom)
  1015. {
  1016. int i;
  1017. if (!dom)
  1018. return;
  1019. del_domain_from_list(&dom->domain);
  1020. free_pagetable(&dom->domain);
  1021. for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
  1022. if (!dom->aperture[i])
  1023. continue;
  1024. free_page((unsigned long)dom->aperture[i]->bitmap);
  1025. kfree(dom->aperture[i]);
  1026. }
  1027. kfree(dom);
  1028. }
  1029. /*
  1030. * Allocates a new protection domain usable for the dma_ops functions.
  1031. * It also intializes the page table and the address allocator data
  1032. * structures required for the dma_ops interface
  1033. */
  1034. static struct dma_ops_domain *dma_ops_domain_alloc(void)
  1035. {
  1036. struct dma_ops_domain *dma_dom;
  1037. dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
  1038. if (!dma_dom)
  1039. return NULL;
  1040. spin_lock_init(&dma_dom->domain.lock);
  1041. dma_dom->domain.id = domain_id_alloc();
  1042. if (dma_dom->domain.id == 0)
  1043. goto free_dma_dom;
  1044. INIT_LIST_HEAD(&dma_dom->domain.dev_list);
  1045. dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
  1046. dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  1047. dma_dom->domain.flags = PD_DMA_OPS_MASK;
  1048. dma_dom->domain.priv = dma_dom;
  1049. if (!dma_dom->domain.pt_root)
  1050. goto free_dma_dom;
  1051. dma_dom->need_flush = false;
  1052. dma_dom->target_dev = 0xffff;
  1053. add_domain_to_list(&dma_dom->domain);
  1054. if (alloc_new_range(dma_dom, true, GFP_KERNEL))
  1055. goto free_dma_dom;
  1056. /*
  1057. * mark the first page as allocated so we never return 0 as
  1058. * a valid dma-address. So we can use 0 as error value
  1059. */
  1060. dma_dom->aperture[0]->bitmap[0] = 1;
  1061. dma_dom->next_address = 0;
  1062. return dma_dom;
  1063. free_dma_dom:
  1064. dma_ops_domain_free(dma_dom);
  1065. return NULL;
  1066. }
  1067. /*
  1068. * little helper function to check whether a given protection domain is a
  1069. * dma_ops domain
  1070. */
  1071. static bool dma_ops_domain(struct protection_domain *domain)
  1072. {
  1073. return domain->flags & PD_DMA_OPS_MASK;
  1074. }
  1075. static void set_dte_entry(u16 devid, struct protection_domain *domain)
  1076. {
  1077. u64 pte_root = virt_to_phys(domain->pt_root);
  1078. pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
  1079. << DEV_ENTRY_MODE_SHIFT;
  1080. pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
  1081. amd_iommu_dev_table[devid].data[2] = domain->id;
  1082. amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
  1083. amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
  1084. }
  1085. static void clear_dte_entry(u16 devid)
  1086. {
  1087. /* remove entry from the device table seen by the hardware */
  1088. amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
  1089. amd_iommu_dev_table[devid].data[1] = 0;
  1090. amd_iommu_dev_table[devid].data[2] = 0;
  1091. amd_iommu_apply_erratum_63(devid);
  1092. }
  1093. static void do_attach(struct device *dev, struct protection_domain *domain)
  1094. {
  1095. struct iommu_dev_data *dev_data;
  1096. struct amd_iommu *iommu;
  1097. u16 devid;
  1098. devid = get_device_id(dev);
  1099. iommu = amd_iommu_rlookup_table[devid];
  1100. dev_data = get_dev_data(dev);
  1101. /* Update data structures */
  1102. dev_data->domain = domain;
  1103. list_add(&dev_data->list, &domain->dev_list);
  1104. set_dte_entry(devid, domain);
  1105. /* Do reference counting */
  1106. domain->dev_iommu[iommu->index] += 1;
  1107. domain->dev_cnt += 1;
  1108. /* Flush the DTE entry */
  1109. iommu_flush_device(dev);
  1110. }
  1111. static void do_detach(struct device *dev)
  1112. {
  1113. struct iommu_dev_data *dev_data;
  1114. struct amd_iommu *iommu;
  1115. u16 devid;
  1116. devid = get_device_id(dev);
  1117. iommu = amd_iommu_rlookup_table[devid];
  1118. dev_data = get_dev_data(dev);
  1119. /* decrease reference counters */
  1120. dev_data->domain->dev_iommu[iommu->index] -= 1;
  1121. dev_data->domain->dev_cnt -= 1;
  1122. /* Update data structures */
  1123. dev_data->domain = NULL;
  1124. list_del(&dev_data->list);
  1125. clear_dte_entry(devid);
  1126. /* Flush the DTE entry */
  1127. iommu_flush_device(dev);
  1128. }
  1129. /*
  1130. * If a device is not yet associated with a domain, this function does
  1131. * assigns it visible for the hardware
  1132. */
  1133. static int __attach_device(struct device *dev,
  1134. struct protection_domain *domain)
  1135. {
  1136. struct iommu_dev_data *dev_data, *alias_data;
  1137. dev_data = get_dev_data(dev);
  1138. alias_data = get_dev_data(dev_data->alias);
  1139. if (!alias_data)
  1140. return -EINVAL;
  1141. /* lock domain */
  1142. spin_lock(&domain->lock);
  1143. /* Some sanity checks */
  1144. if (alias_data->domain != NULL &&
  1145. alias_data->domain != domain)
  1146. return -EBUSY;
  1147. if (dev_data->domain != NULL &&
  1148. dev_data->domain != domain)
  1149. return -EBUSY;
  1150. /* Do real assignment */
  1151. if (dev_data->alias != dev) {
  1152. alias_data = get_dev_data(dev_data->alias);
  1153. if (alias_data->domain == NULL)
  1154. do_attach(dev_data->alias, domain);
  1155. atomic_inc(&alias_data->bind);
  1156. }
  1157. if (dev_data->domain == NULL)
  1158. do_attach(dev, domain);
  1159. atomic_inc(&dev_data->bind);
  1160. /* ready */
  1161. spin_unlock(&domain->lock);
  1162. return 0;
  1163. }
  1164. /*
  1165. * If a device is not yet associated with a domain, this function does
  1166. * assigns it visible for the hardware
  1167. */
  1168. static int attach_device(struct device *dev,
  1169. struct protection_domain *domain)
  1170. {
  1171. unsigned long flags;
  1172. int ret;
  1173. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1174. ret = __attach_device(dev, domain);
  1175. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1176. /*
  1177. * We might boot into a crash-kernel here. The crashed kernel
  1178. * left the caches in the IOMMU dirty. So we have to flush
  1179. * here to evict all dirty stuff.
  1180. */
  1181. iommu_flush_tlb_pde(domain);
  1182. return ret;
  1183. }
  1184. /*
  1185. * Removes a device from a protection domain (unlocked)
  1186. */
  1187. static void __detach_device(struct device *dev)
  1188. {
  1189. struct iommu_dev_data *dev_data = get_dev_data(dev);
  1190. struct iommu_dev_data *alias_data;
  1191. struct protection_domain *domain;
  1192. unsigned long flags;
  1193. BUG_ON(!dev_data->domain);
  1194. domain = dev_data->domain;
  1195. spin_lock_irqsave(&domain->lock, flags);
  1196. if (dev_data->alias != dev) {
  1197. alias_data = get_dev_data(dev_data->alias);
  1198. if (atomic_dec_and_test(&alias_data->bind))
  1199. do_detach(dev_data->alias);
  1200. }
  1201. if (atomic_dec_and_test(&dev_data->bind))
  1202. do_detach(dev);
  1203. spin_unlock_irqrestore(&domain->lock, flags);
  1204. /*
  1205. * If we run in passthrough mode the device must be assigned to the
  1206. * passthrough domain if it is detached from any other domain.
  1207. * Make sure we can deassign from the pt_domain itself.
  1208. */
  1209. if (iommu_pass_through &&
  1210. (dev_data->domain == NULL && domain != pt_domain))
  1211. __attach_device(dev, pt_domain);
  1212. }
  1213. /*
  1214. * Removes a device from a protection domain (with devtable_lock held)
  1215. */
  1216. static void detach_device(struct device *dev)
  1217. {
  1218. unsigned long flags;
  1219. /* lock device table */
  1220. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1221. __detach_device(dev);
  1222. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1223. }
  1224. /*
  1225. * Find out the protection domain structure for a given PCI device. This
  1226. * will give us the pointer to the page table root for example.
  1227. */
  1228. static struct protection_domain *domain_for_device(struct device *dev)
  1229. {
  1230. struct protection_domain *dom;
  1231. struct iommu_dev_data *dev_data, *alias_data;
  1232. unsigned long flags;
  1233. u16 devid, alias;
  1234. devid = get_device_id(dev);
  1235. alias = amd_iommu_alias_table[devid];
  1236. dev_data = get_dev_data(dev);
  1237. alias_data = get_dev_data(dev_data->alias);
  1238. if (!alias_data)
  1239. return NULL;
  1240. read_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1241. dom = dev_data->domain;
  1242. if (dom == NULL &&
  1243. alias_data->domain != NULL) {
  1244. __attach_device(dev, alias_data->domain);
  1245. dom = alias_data->domain;
  1246. }
  1247. read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1248. return dom;
  1249. }
  1250. static int device_change_notifier(struct notifier_block *nb,
  1251. unsigned long action, void *data)
  1252. {
  1253. struct device *dev = data;
  1254. u16 devid;
  1255. struct protection_domain *domain;
  1256. struct dma_ops_domain *dma_domain;
  1257. struct amd_iommu *iommu;
  1258. unsigned long flags;
  1259. if (!check_device(dev))
  1260. return 0;
  1261. devid = get_device_id(dev);
  1262. iommu = amd_iommu_rlookup_table[devid];
  1263. switch (action) {
  1264. case BUS_NOTIFY_UNBOUND_DRIVER:
  1265. domain = domain_for_device(dev);
  1266. if (!domain)
  1267. goto out;
  1268. if (iommu_pass_through)
  1269. break;
  1270. detach_device(dev);
  1271. break;
  1272. case BUS_NOTIFY_ADD_DEVICE:
  1273. iommu_init_device(dev);
  1274. domain = domain_for_device(dev);
  1275. /* allocate a protection domain if a device is added */
  1276. dma_domain = find_protection_domain(devid);
  1277. if (dma_domain)
  1278. goto out;
  1279. dma_domain = dma_ops_domain_alloc();
  1280. if (!dma_domain)
  1281. goto out;
  1282. dma_domain->target_dev = devid;
  1283. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  1284. list_add_tail(&dma_domain->list, &iommu_pd_list);
  1285. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  1286. break;
  1287. case BUS_NOTIFY_DEL_DEVICE:
  1288. iommu_uninit_device(dev);
  1289. default:
  1290. goto out;
  1291. }
  1292. iommu_flush_device(dev);
  1293. iommu_completion_wait(iommu);
  1294. out:
  1295. return 0;
  1296. }
  1297. static struct notifier_block device_nb = {
  1298. .notifier_call = device_change_notifier,
  1299. };
  1300. void amd_iommu_init_notifier(void)
  1301. {
  1302. bus_register_notifier(&pci_bus_type, &device_nb);
  1303. }
  1304. /*****************************************************************************
  1305. *
  1306. * The next functions belong to the dma_ops mapping/unmapping code.
  1307. *
  1308. *****************************************************************************/
  1309. /*
  1310. * In the dma_ops path we only have the struct device. This function
  1311. * finds the corresponding IOMMU, the protection domain and the
  1312. * requestor id for a given device.
  1313. * If the device is not yet associated with a domain this is also done
  1314. * in this function.
  1315. */
  1316. static struct protection_domain *get_domain(struct device *dev)
  1317. {
  1318. struct protection_domain *domain;
  1319. struct dma_ops_domain *dma_dom;
  1320. u16 devid = get_device_id(dev);
  1321. if (!check_device(dev))
  1322. return ERR_PTR(-EINVAL);
  1323. domain = domain_for_device(dev);
  1324. if (domain != NULL && !dma_ops_domain(domain))
  1325. return ERR_PTR(-EBUSY);
  1326. if (domain != NULL)
  1327. return domain;
  1328. /* Device not bount yet - bind it */
  1329. dma_dom = find_protection_domain(devid);
  1330. if (!dma_dom)
  1331. dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
  1332. attach_device(dev, &dma_dom->domain);
  1333. DUMP_printk("Using protection domain %d for device %s\n",
  1334. dma_dom->domain.id, dev_name(dev));
  1335. return &dma_dom->domain;
  1336. }
  1337. static void update_device_table(struct protection_domain *domain)
  1338. {
  1339. struct iommu_dev_data *dev_data;
  1340. list_for_each_entry(dev_data, &domain->dev_list, list) {
  1341. u16 devid = get_device_id(dev_data->dev);
  1342. set_dte_entry(devid, domain);
  1343. }
  1344. }
  1345. static void update_domain(struct protection_domain *domain)
  1346. {
  1347. if (!domain->updated)
  1348. return;
  1349. update_device_table(domain);
  1350. iommu_flush_domain_devices(domain);
  1351. iommu_flush_tlb_pde(domain);
  1352. domain->updated = false;
  1353. }
  1354. /*
  1355. * This function fetches the PTE for a given address in the aperture
  1356. */
  1357. static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
  1358. unsigned long address)
  1359. {
  1360. struct aperture_range *aperture;
  1361. u64 *pte, *pte_page;
  1362. aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
  1363. if (!aperture)
  1364. return NULL;
  1365. pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
  1366. if (!pte) {
  1367. pte = alloc_pte(&dom->domain, address, PM_MAP_4k, &pte_page,
  1368. GFP_ATOMIC);
  1369. aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
  1370. } else
  1371. pte += PM_LEVEL_INDEX(0, address);
  1372. update_domain(&dom->domain);
  1373. return pte;
  1374. }
  1375. /*
  1376. * This is the generic map function. It maps one 4kb page at paddr to
  1377. * the given address in the DMA address space for the domain.
  1378. */
  1379. static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
  1380. unsigned long address,
  1381. phys_addr_t paddr,
  1382. int direction)
  1383. {
  1384. u64 *pte, __pte;
  1385. WARN_ON(address > dom->aperture_size);
  1386. paddr &= PAGE_MASK;
  1387. pte = dma_ops_get_pte(dom, address);
  1388. if (!pte)
  1389. return DMA_ERROR_CODE;
  1390. __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
  1391. if (direction == DMA_TO_DEVICE)
  1392. __pte |= IOMMU_PTE_IR;
  1393. else if (direction == DMA_FROM_DEVICE)
  1394. __pte |= IOMMU_PTE_IW;
  1395. else if (direction == DMA_BIDIRECTIONAL)
  1396. __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
  1397. WARN_ON(*pte);
  1398. *pte = __pte;
  1399. return (dma_addr_t)address;
  1400. }
  1401. /*
  1402. * The generic unmapping function for on page in the DMA address space.
  1403. */
  1404. static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
  1405. unsigned long address)
  1406. {
  1407. struct aperture_range *aperture;
  1408. u64 *pte;
  1409. if (address >= dom->aperture_size)
  1410. return;
  1411. aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
  1412. if (!aperture)
  1413. return;
  1414. pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
  1415. if (!pte)
  1416. return;
  1417. pte += PM_LEVEL_INDEX(0, address);
  1418. WARN_ON(!*pte);
  1419. *pte = 0ULL;
  1420. }
  1421. /*
  1422. * This function contains common code for mapping of a physically
  1423. * contiguous memory region into DMA address space. It is used by all
  1424. * mapping functions provided with this IOMMU driver.
  1425. * Must be called with the domain lock held.
  1426. */
  1427. static dma_addr_t __map_single(struct device *dev,
  1428. struct dma_ops_domain *dma_dom,
  1429. phys_addr_t paddr,
  1430. size_t size,
  1431. int dir,
  1432. bool align,
  1433. u64 dma_mask)
  1434. {
  1435. dma_addr_t offset = paddr & ~PAGE_MASK;
  1436. dma_addr_t address, start, ret;
  1437. unsigned int pages;
  1438. unsigned long align_mask = 0;
  1439. int i;
  1440. pages = iommu_num_pages(paddr, size, PAGE_SIZE);
  1441. paddr &= PAGE_MASK;
  1442. INC_STATS_COUNTER(total_map_requests);
  1443. if (pages > 1)
  1444. INC_STATS_COUNTER(cross_page);
  1445. if (align)
  1446. align_mask = (1UL << get_order(size)) - 1;
  1447. retry:
  1448. address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
  1449. dma_mask);
  1450. if (unlikely(address == DMA_ERROR_CODE)) {
  1451. /*
  1452. * setting next_address here will let the address
  1453. * allocator only scan the new allocated range in the
  1454. * first run. This is a small optimization.
  1455. */
  1456. dma_dom->next_address = dma_dom->aperture_size;
  1457. if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
  1458. goto out;
  1459. /*
  1460. * aperture was successfully enlarged by 128 MB, try
  1461. * allocation again
  1462. */
  1463. goto retry;
  1464. }
  1465. start = address;
  1466. for (i = 0; i < pages; ++i) {
  1467. ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
  1468. if (ret == DMA_ERROR_CODE)
  1469. goto out_unmap;
  1470. paddr += PAGE_SIZE;
  1471. start += PAGE_SIZE;
  1472. }
  1473. address += offset;
  1474. ADD_STATS_COUNTER(alloced_io_mem, size);
  1475. if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
  1476. iommu_flush_tlb(&dma_dom->domain);
  1477. dma_dom->need_flush = false;
  1478. } else if (unlikely(amd_iommu_np_cache))
  1479. iommu_flush_pages(&dma_dom->domain, address, size);
  1480. out:
  1481. return address;
  1482. out_unmap:
  1483. for (--i; i >= 0; --i) {
  1484. start -= PAGE_SIZE;
  1485. dma_ops_domain_unmap(dma_dom, start);
  1486. }
  1487. dma_ops_free_addresses(dma_dom, address, pages);
  1488. return DMA_ERROR_CODE;
  1489. }
  1490. /*
  1491. * Does the reverse of the __map_single function. Must be called with
  1492. * the domain lock held too
  1493. */
  1494. static void __unmap_single(struct dma_ops_domain *dma_dom,
  1495. dma_addr_t dma_addr,
  1496. size_t size,
  1497. int dir)
  1498. {
  1499. dma_addr_t i, start;
  1500. unsigned int pages;
  1501. if ((dma_addr == DMA_ERROR_CODE) ||
  1502. (dma_addr + size > dma_dom->aperture_size))
  1503. return;
  1504. pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  1505. dma_addr &= PAGE_MASK;
  1506. start = dma_addr;
  1507. for (i = 0; i < pages; ++i) {
  1508. dma_ops_domain_unmap(dma_dom, start);
  1509. start += PAGE_SIZE;
  1510. }
  1511. SUB_STATS_COUNTER(alloced_io_mem, size);
  1512. dma_ops_free_addresses(dma_dom, dma_addr, pages);
  1513. if (amd_iommu_unmap_flush || dma_dom->need_flush) {
  1514. iommu_flush_pages(&dma_dom->domain, dma_addr, size);
  1515. dma_dom->need_flush = false;
  1516. }
  1517. }
  1518. /*
  1519. * The exported map_single function for dma_ops.
  1520. */
  1521. static dma_addr_t map_page(struct device *dev, struct page *page,
  1522. unsigned long offset, size_t size,
  1523. enum dma_data_direction dir,
  1524. struct dma_attrs *attrs)
  1525. {
  1526. unsigned long flags;
  1527. struct protection_domain *domain;
  1528. dma_addr_t addr;
  1529. u64 dma_mask;
  1530. phys_addr_t paddr = page_to_phys(page) + offset;
  1531. INC_STATS_COUNTER(cnt_map_single);
  1532. domain = get_domain(dev);
  1533. if (PTR_ERR(domain) == -EINVAL)
  1534. return (dma_addr_t)paddr;
  1535. else if (IS_ERR(domain))
  1536. return DMA_ERROR_CODE;
  1537. dma_mask = *dev->dma_mask;
  1538. spin_lock_irqsave(&domain->lock, flags);
  1539. addr = __map_single(dev, domain->priv, paddr, size, dir, false,
  1540. dma_mask);
  1541. if (addr == DMA_ERROR_CODE)
  1542. goto out;
  1543. iommu_flush_complete(domain);
  1544. out:
  1545. spin_unlock_irqrestore(&domain->lock, flags);
  1546. return addr;
  1547. }
  1548. /*
  1549. * The exported unmap_single function for dma_ops.
  1550. */
  1551. static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
  1552. enum dma_data_direction dir, struct dma_attrs *attrs)
  1553. {
  1554. unsigned long flags;
  1555. struct protection_domain *domain;
  1556. INC_STATS_COUNTER(cnt_unmap_single);
  1557. domain = get_domain(dev);
  1558. if (IS_ERR(domain))
  1559. return;
  1560. spin_lock_irqsave(&domain->lock, flags);
  1561. __unmap_single(domain->priv, dma_addr, size, dir);
  1562. iommu_flush_complete(domain);
  1563. spin_unlock_irqrestore(&domain->lock, flags);
  1564. }
  1565. /*
  1566. * This is a special map_sg function which is used if we should map a
  1567. * device which is not handled by an AMD IOMMU in the system.
  1568. */
  1569. static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
  1570. int nelems, int dir)
  1571. {
  1572. struct scatterlist *s;
  1573. int i;
  1574. for_each_sg(sglist, s, nelems, i) {
  1575. s->dma_address = (dma_addr_t)sg_phys(s);
  1576. s->dma_length = s->length;
  1577. }
  1578. return nelems;
  1579. }
  1580. /*
  1581. * The exported map_sg function for dma_ops (handles scatter-gather
  1582. * lists).
  1583. */
  1584. static int map_sg(struct device *dev, struct scatterlist *sglist,
  1585. int nelems, enum dma_data_direction dir,
  1586. struct dma_attrs *attrs)
  1587. {
  1588. unsigned long flags;
  1589. struct protection_domain *domain;
  1590. int i;
  1591. struct scatterlist *s;
  1592. phys_addr_t paddr;
  1593. int mapped_elems = 0;
  1594. u64 dma_mask;
  1595. INC_STATS_COUNTER(cnt_map_sg);
  1596. domain = get_domain(dev);
  1597. if (PTR_ERR(domain) == -EINVAL)
  1598. return map_sg_no_iommu(dev, sglist, nelems, dir);
  1599. else if (IS_ERR(domain))
  1600. return 0;
  1601. dma_mask = *dev->dma_mask;
  1602. spin_lock_irqsave(&domain->lock, flags);
  1603. for_each_sg(sglist, s, nelems, i) {
  1604. paddr = sg_phys(s);
  1605. s->dma_address = __map_single(dev, domain->priv,
  1606. paddr, s->length, dir, false,
  1607. dma_mask);
  1608. if (s->dma_address) {
  1609. s->dma_length = s->length;
  1610. mapped_elems++;
  1611. } else
  1612. goto unmap;
  1613. }
  1614. iommu_flush_complete(domain);
  1615. out:
  1616. spin_unlock_irqrestore(&domain->lock, flags);
  1617. return mapped_elems;
  1618. unmap:
  1619. for_each_sg(sglist, s, mapped_elems, i) {
  1620. if (s->dma_address)
  1621. __unmap_single(domain->priv, s->dma_address,
  1622. s->dma_length, dir);
  1623. s->dma_address = s->dma_length = 0;
  1624. }
  1625. mapped_elems = 0;
  1626. goto out;
  1627. }
  1628. /*
  1629. * The exported map_sg function for dma_ops (handles scatter-gather
  1630. * lists).
  1631. */
  1632. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  1633. int nelems, enum dma_data_direction dir,
  1634. struct dma_attrs *attrs)
  1635. {
  1636. unsigned long flags;
  1637. struct protection_domain *domain;
  1638. struct scatterlist *s;
  1639. int i;
  1640. INC_STATS_COUNTER(cnt_unmap_sg);
  1641. domain = get_domain(dev);
  1642. if (IS_ERR(domain))
  1643. return;
  1644. spin_lock_irqsave(&domain->lock, flags);
  1645. for_each_sg(sglist, s, nelems, i) {
  1646. __unmap_single(domain->priv, s->dma_address,
  1647. s->dma_length, dir);
  1648. s->dma_address = s->dma_length = 0;
  1649. }
  1650. iommu_flush_complete(domain);
  1651. spin_unlock_irqrestore(&domain->lock, flags);
  1652. }
  1653. /*
  1654. * The exported alloc_coherent function for dma_ops.
  1655. */
  1656. static void *alloc_coherent(struct device *dev, size_t size,
  1657. dma_addr_t *dma_addr, gfp_t flag)
  1658. {
  1659. unsigned long flags;
  1660. void *virt_addr;
  1661. struct protection_domain *domain;
  1662. phys_addr_t paddr;
  1663. u64 dma_mask = dev->coherent_dma_mask;
  1664. INC_STATS_COUNTER(cnt_alloc_coherent);
  1665. domain = get_domain(dev);
  1666. if (PTR_ERR(domain) == -EINVAL) {
  1667. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  1668. *dma_addr = __pa(virt_addr);
  1669. return virt_addr;
  1670. } else if (IS_ERR(domain))
  1671. return NULL;
  1672. dma_mask = dev->coherent_dma_mask;
  1673. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  1674. flag |= __GFP_ZERO;
  1675. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  1676. if (!virt_addr)
  1677. return NULL;
  1678. paddr = virt_to_phys(virt_addr);
  1679. if (!dma_mask)
  1680. dma_mask = *dev->dma_mask;
  1681. spin_lock_irqsave(&domain->lock, flags);
  1682. *dma_addr = __map_single(dev, domain->priv, paddr,
  1683. size, DMA_BIDIRECTIONAL, true, dma_mask);
  1684. if (*dma_addr == DMA_ERROR_CODE) {
  1685. spin_unlock_irqrestore(&domain->lock, flags);
  1686. goto out_free;
  1687. }
  1688. iommu_flush_complete(domain);
  1689. spin_unlock_irqrestore(&domain->lock, flags);
  1690. return virt_addr;
  1691. out_free:
  1692. free_pages((unsigned long)virt_addr, get_order(size));
  1693. return NULL;
  1694. }
  1695. /*
  1696. * The exported free_coherent function for dma_ops.
  1697. */
  1698. static void free_coherent(struct device *dev, size_t size,
  1699. void *virt_addr, dma_addr_t dma_addr)
  1700. {
  1701. unsigned long flags;
  1702. struct protection_domain *domain;
  1703. INC_STATS_COUNTER(cnt_free_coherent);
  1704. domain = get_domain(dev);
  1705. if (IS_ERR(domain))
  1706. goto free_mem;
  1707. spin_lock_irqsave(&domain->lock, flags);
  1708. __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
  1709. iommu_flush_complete(domain);
  1710. spin_unlock_irqrestore(&domain->lock, flags);
  1711. free_mem:
  1712. free_pages((unsigned long)virt_addr, get_order(size));
  1713. }
  1714. /*
  1715. * This function is called by the DMA layer to find out if we can handle a
  1716. * particular device. It is part of the dma_ops.
  1717. */
  1718. static int amd_iommu_dma_supported(struct device *dev, u64 mask)
  1719. {
  1720. return check_device(dev);
  1721. }
  1722. /*
  1723. * The function for pre-allocating protection domains.
  1724. *
  1725. * If the driver core informs the DMA layer if a driver grabs a device
  1726. * we don't need to preallocate the protection domains anymore.
  1727. * For now we have to.
  1728. */
  1729. static void prealloc_protection_domains(void)
  1730. {
  1731. struct pci_dev *dev = NULL;
  1732. struct dma_ops_domain *dma_dom;
  1733. u16 devid;
  1734. for_each_pci_dev(dev) {
  1735. /* Do we handle this device? */
  1736. if (!check_device(&dev->dev))
  1737. continue;
  1738. /* Is there already any domain for it? */
  1739. if (domain_for_device(&dev->dev))
  1740. continue;
  1741. devid = get_device_id(&dev->dev);
  1742. dma_dom = dma_ops_domain_alloc();
  1743. if (!dma_dom)
  1744. continue;
  1745. init_unity_mappings_for_device(dma_dom, devid);
  1746. dma_dom->target_dev = devid;
  1747. attach_device(&dev->dev, &dma_dom->domain);
  1748. list_add_tail(&dma_dom->list, &iommu_pd_list);
  1749. }
  1750. }
  1751. static struct dma_map_ops amd_iommu_dma_ops = {
  1752. .alloc_coherent = alloc_coherent,
  1753. .free_coherent = free_coherent,
  1754. .map_page = map_page,
  1755. .unmap_page = unmap_page,
  1756. .map_sg = map_sg,
  1757. .unmap_sg = unmap_sg,
  1758. .dma_supported = amd_iommu_dma_supported,
  1759. };
  1760. /*
  1761. * The function which clues the AMD IOMMU driver into dma_ops.
  1762. */
  1763. void __init amd_iommu_init_api(void)
  1764. {
  1765. register_iommu(&amd_iommu_ops);
  1766. }
  1767. int __init amd_iommu_init_dma_ops(void)
  1768. {
  1769. struct amd_iommu *iommu;
  1770. int ret;
  1771. /*
  1772. * first allocate a default protection domain for every IOMMU we
  1773. * found in the system. Devices not assigned to any other
  1774. * protection domain will be assigned to the default one.
  1775. */
  1776. for_each_iommu(iommu) {
  1777. iommu->default_dom = dma_ops_domain_alloc();
  1778. if (iommu->default_dom == NULL)
  1779. return -ENOMEM;
  1780. iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
  1781. ret = iommu_init_unity_mappings(iommu);
  1782. if (ret)
  1783. goto free_domains;
  1784. }
  1785. /*
  1786. * Pre-allocate the protection domains for each device.
  1787. */
  1788. prealloc_protection_domains();
  1789. iommu_detected = 1;
  1790. swiotlb = 0;
  1791. #ifdef CONFIG_GART_IOMMU
  1792. gart_iommu_aperture_disabled = 1;
  1793. gart_iommu_aperture = 0;
  1794. #endif
  1795. /* Make the driver finally visible to the drivers */
  1796. dma_ops = &amd_iommu_dma_ops;
  1797. amd_iommu_stats_init();
  1798. return 0;
  1799. free_domains:
  1800. for_each_iommu(iommu) {
  1801. if (iommu->default_dom)
  1802. dma_ops_domain_free(iommu->default_dom);
  1803. }
  1804. return ret;
  1805. }
  1806. /*****************************************************************************
  1807. *
  1808. * The following functions belong to the exported interface of AMD IOMMU
  1809. *
  1810. * This interface allows access to lower level functions of the IOMMU
  1811. * like protection domain handling and assignement of devices to domains
  1812. * which is not possible with the dma_ops interface.
  1813. *
  1814. *****************************************************************************/
  1815. static void cleanup_domain(struct protection_domain *domain)
  1816. {
  1817. struct iommu_dev_data *dev_data, *next;
  1818. unsigned long flags;
  1819. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1820. list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
  1821. struct device *dev = dev_data->dev;
  1822. __detach_device(dev);
  1823. atomic_set(&dev_data->bind, 0);
  1824. }
  1825. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1826. }
  1827. static void protection_domain_free(struct protection_domain *domain)
  1828. {
  1829. if (!domain)
  1830. return;
  1831. del_domain_from_list(domain);
  1832. if (domain->id)
  1833. domain_id_free(domain->id);
  1834. kfree(domain);
  1835. }
  1836. static struct protection_domain *protection_domain_alloc(void)
  1837. {
  1838. struct protection_domain *domain;
  1839. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  1840. if (!domain)
  1841. return NULL;
  1842. spin_lock_init(&domain->lock);
  1843. mutex_init(&domain->api_lock);
  1844. domain->id = domain_id_alloc();
  1845. if (!domain->id)
  1846. goto out_err;
  1847. INIT_LIST_HEAD(&domain->dev_list);
  1848. add_domain_to_list(domain);
  1849. return domain;
  1850. out_err:
  1851. kfree(domain);
  1852. return NULL;
  1853. }
  1854. static int amd_iommu_domain_init(struct iommu_domain *dom)
  1855. {
  1856. struct protection_domain *domain;
  1857. domain = protection_domain_alloc();
  1858. if (!domain)
  1859. goto out_free;
  1860. domain->mode = PAGE_MODE_3_LEVEL;
  1861. domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  1862. if (!domain->pt_root)
  1863. goto out_free;
  1864. dom->priv = domain;
  1865. return 0;
  1866. out_free:
  1867. protection_domain_free(domain);
  1868. return -ENOMEM;
  1869. }
  1870. static void amd_iommu_domain_destroy(struct iommu_domain *dom)
  1871. {
  1872. struct protection_domain *domain = dom->priv;
  1873. if (!domain)
  1874. return;
  1875. if (domain->dev_cnt > 0)
  1876. cleanup_domain(domain);
  1877. BUG_ON(domain->dev_cnt != 0);
  1878. free_pagetable(domain);
  1879. protection_domain_free(domain);
  1880. dom->priv = NULL;
  1881. }
  1882. static void amd_iommu_detach_device(struct iommu_domain *dom,
  1883. struct device *dev)
  1884. {
  1885. struct iommu_dev_data *dev_data = dev->archdata.iommu;
  1886. struct amd_iommu *iommu;
  1887. u16 devid;
  1888. if (!check_device(dev))
  1889. return;
  1890. devid = get_device_id(dev);
  1891. if (dev_data->domain != NULL)
  1892. detach_device(dev);
  1893. iommu = amd_iommu_rlookup_table[devid];
  1894. if (!iommu)
  1895. return;
  1896. iommu_flush_device(dev);
  1897. iommu_completion_wait(iommu);
  1898. }
  1899. static int amd_iommu_attach_device(struct iommu_domain *dom,
  1900. struct device *dev)
  1901. {
  1902. struct protection_domain *domain = dom->priv;
  1903. struct iommu_dev_data *dev_data;
  1904. struct amd_iommu *iommu;
  1905. int ret;
  1906. u16 devid;
  1907. if (!check_device(dev))
  1908. return -EINVAL;
  1909. dev_data = dev->archdata.iommu;
  1910. devid = get_device_id(dev);
  1911. iommu = amd_iommu_rlookup_table[devid];
  1912. if (!iommu)
  1913. return -EINVAL;
  1914. if (dev_data->domain)
  1915. detach_device(dev);
  1916. ret = attach_device(dev, domain);
  1917. iommu_completion_wait(iommu);
  1918. return ret;
  1919. }
  1920. static int amd_iommu_map_range(struct iommu_domain *dom,
  1921. unsigned long iova, phys_addr_t paddr,
  1922. size_t size, int iommu_prot)
  1923. {
  1924. struct protection_domain *domain = dom->priv;
  1925. unsigned long i, npages = iommu_num_pages(paddr, size, PAGE_SIZE);
  1926. int prot = 0;
  1927. int ret;
  1928. if (iommu_prot & IOMMU_READ)
  1929. prot |= IOMMU_PROT_IR;
  1930. if (iommu_prot & IOMMU_WRITE)
  1931. prot |= IOMMU_PROT_IW;
  1932. iova &= PAGE_MASK;
  1933. paddr &= PAGE_MASK;
  1934. mutex_lock(&domain->api_lock);
  1935. for (i = 0; i < npages; ++i) {
  1936. ret = iommu_map_page(domain, iova, paddr, prot, PM_MAP_4k);
  1937. if (ret)
  1938. return ret;
  1939. iova += PAGE_SIZE;
  1940. paddr += PAGE_SIZE;
  1941. }
  1942. mutex_unlock(&domain->api_lock);
  1943. return 0;
  1944. }
  1945. static void amd_iommu_unmap_range(struct iommu_domain *dom,
  1946. unsigned long iova, size_t size)
  1947. {
  1948. struct protection_domain *domain = dom->priv;
  1949. unsigned long i, npages = iommu_num_pages(iova, size, PAGE_SIZE);
  1950. iova &= PAGE_MASK;
  1951. mutex_lock(&domain->api_lock);
  1952. for (i = 0; i < npages; ++i) {
  1953. iommu_unmap_page(domain, iova, PM_MAP_4k);
  1954. iova += PAGE_SIZE;
  1955. }
  1956. iommu_flush_tlb_pde(domain);
  1957. mutex_unlock(&domain->api_lock);
  1958. }
  1959. static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
  1960. unsigned long iova)
  1961. {
  1962. struct protection_domain *domain = dom->priv;
  1963. unsigned long offset = iova & ~PAGE_MASK;
  1964. phys_addr_t paddr;
  1965. u64 *pte;
  1966. pte = fetch_pte(domain, iova, PM_MAP_4k);
  1967. if (!pte || !IOMMU_PTE_PRESENT(*pte))
  1968. return 0;
  1969. paddr = *pte & IOMMU_PAGE_MASK;
  1970. paddr |= offset;
  1971. return paddr;
  1972. }
  1973. static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
  1974. unsigned long cap)
  1975. {
  1976. return 0;
  1977. }
  1978. static struct iommu_ops amd_iommu_ops = {
  1979. .domain_init = amd_iommu_domain_init,
  1980. .domain_destroy = amd_iommu_domain_destroy,
  1981. .attach_dev = amd_iommu_attach_device,
  1982. .detach_dev = amd_iommu_detach_device,
  1983. .map = amd_iommu_map_range,
  1984. .unmap = amd_iommu_unmap_range,
  1985. .iova_to_phys = amd_iommu_iova_to_phys,
  1986. .domain_has_cap = amd_iommu_domain_has_cap,
  1987. };
  1988. /*****************************************************************************
  1989. *
  1990. * The next functions do a basic initialization of IOMMU for pass through
  1991. * mode
  1992. *
  1993. * In passthrough mode the IOMMU is initialized and enabled but not used for
  1994. * DMA-API translation.
  1995. *
  1996. *****************************************************************************/
  1997. int __init amd_iommu_init_passthrough(void)
  1998. {
  1999. struct amd_iommu *iommu;
  2000. struct pci_dev *dev = NULL;
  2001. u16 devid;
  2002. /* allocate passthrough domain */
  2003. pt_domain = protection_domain_alloc();
  2004. if (!pt_domain)
  2005. return -ENOMEM;
  2006. pt_domain->mode |= PAGE_MODE_NONE;
  2007. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  2008. if (!check_device(&dev->dev))
  2009. continue;
  2010. devid = get_device_id(&dev->dev);
  2011. iommu = amd_iommu_rlookup_table[devid];
  2012. if (!iommu)
  2013. continue;
  2014. attach_device(&dev->dev, pt_domain);
  2015. }
  2016. pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
  2017. return 0;
  2018. }