amd_iommu.c 59 KB

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