amd_iommu.c 59 KB

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