amd_iommu.c 58 KB

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