amd_iommu.c 62 KB

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