dmar.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015
  1. /*
  2. * Copyright (c) 2006, Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Copyright (C) 2006-2008 Intel Corporation
  18. * Author: Ashok Raj <ashok.raj@intel.com>
  19. * Author: Shaohua Li <shaohua.li@intel.com>
  20. * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
  21. *
  22. * This file implements early detection/parsing of Remapping Devices
  23. * reported to OS through BIOS via DMA remapping reporting (DMAR) ACPI
  24. * tables.
  25. *
  26. * These routines are used by both DMA-remapping and Interrupt-remapping
  27. */
  28. #define pr_fmt(fmt) "DMAR: " fmt
  29. #include <linux/pci.h>
  30. #include <linux/dmar.h>
  31. #include <linux/iova.h>
  32. #include <linux/intel-iommu.h>
  33. #include <linux/timer.h>
  34. #include <linux/irq.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/tboot.h>
  37. #include <linux/dmi.h>
  38. #include <linux/slab.h>
  39. #include <linux/iommu.h>
  40. #include <asm/irq_remapping.h>
  41. #include <asm/iommu_table.h>
  42. #include "irq_remapping.h"
  43. typedef int (*dmar_res_handler_t)(struct acpi_dmar_header *, void *);
  44. struct dmar_res_callback {
  45. dmar_res_handler_t cb[ACPI_DMAR_TYPE_RESERVED];
  46. void *arg[ACPI_DMAR_TYPE_RESERVED];
  47. bool ignore_unhandled;
  48. bool print_entry;
  49. };
  50. /*
  51. * Assumptions:
  52. * 1) The hotplug framework guarentees that DMAR unit will be hot-added
  53. * before IO devices managed by that unit.
  54. * 2) The hotplug framework guarantees that DMAR unit will be hot-removed
  55. * after IO devices managed by that unit.
  56. * 3) Hotplug events are rare.
  57. *
  58. * Locking rules for DMA and interrupt remapping related global data structures:
  59. * 1) Use dmar_global_lock in process context
  60. * 2) Use RCU in interrupt context
  61. */
  62. DECLARE_RWSEM(dmar_global_lock);
  63. LIST_HEAD(dmar_drhd_units);
  64. struct acpi_table_header * __initdata dmar_tbl;
  65. static acpi_size dmar_tbl_size;
  66. static int dmar_dev_scope_status = 1;
  67. static unsigned long dmar_seq_ids[BITS_TO_LONGS(DMAR_UNITS_SUPPORTED)];
  68. static int alloc_iommu(struct dmar_drhd_unit *drhd);
  69. static void free_iommu(struct intel_iommu *iommu);
  70. static void dmar_register_drhd_unit(struct dmar_drhd_unit *drhd)
  71. {
  72. /*
  73. * add INCLUDE_ALL at the tail, so scan the list will find it at
  74. * the very end.
  75. */
  76. if (drhd->include_all)
  77. list_add_tail_rcu(&drhd->list, &dmar_drhd_units);
  78. else
  79. list_add_rcu(&drhd->list, &dmar_drhd_units);
  80. }
  81. void *dmar_alloc_dev_scope(void *start, void *end, int *cnt)
  82. {
  83. struct acpi_dmar_device_scope *scope;
  84. *cnt = 0;
  85. while (start < end) {
  86. scope = start;
  87. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_NAMESPACE ||
  88. scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT ||
  89. scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE)
  90. (*cnt)++;
  91. else if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_IOAPIC &&
  92. scope->entry_type != ACPI_DMAR_SCOPE_TYPE_HPET) {
  93. pr_warn("Unsupported device scope\n");
  94. }
  95. start += scope->length;
  96. }
  97. if (*cnt == 0)
  98. return NULL;
  99. return kcalloc(*cnt, sizeof(struct dmar_dev_scope), GFP_KERNEL);
  100. }
  101. void dmar_free_dev_scope(struct dmar_dev_scope **devices, int *cnt)
  102. {
  103. int i;
  104. struct device *tmp_dev;
  105. if (*devices && *cnt) {
  106. for_each_active_dev_scope(*devices, *cnt, i, tmp_dev)
  107. put_device(tmp_dev);
  108. kfree(*devices);
  109. }
  110. *devices = NULL;
  111. *cnt = 0;
  112. }
  113. /* Optimize out kzalloc()/kfree() for normal cases */
  114. static char dmar_pci_notify_info_buf[64];
  115. static struct dmar_pci_notify_info *
  116. dmar_alloc_pci_notify_info(struct pci_dev *dev, unsigned long event)
  117. {
  118. int level = 0;
  119. size_t size;
  120. struct pci_dev *tmp;
  121. struct dmar_pci_notify_info *info;
  122. BUG_ON(dev->is_virtfn);
  123. /* Only generate path[] for device addition event */
  124. if (event == BUS_NOTIFY_ADD_DEVICE)
  125. for (tmp = dev; tmp; tmp = tmp->bus->self)
  126. level++;
  127. size = sizeof(*info) + level * sizeof(info->path[0]);
  128. if (size <= sizeof(dmar_pci_notify_info_buf)) {
  129. info = (struct dmar_pci_notify_info *)dmar_pci_notify_info_buf;
  130. } else {
  131. info = kzalloc(size, GFP_KERNEL);
  132. if (!info) {
  133. pr_warn("Out of memory when allocating notify_info "
  134. "for %s.\n", pci_name(dev));
  135. if (dmar_dev_scope_status == 0)
  136. dmar_dev_scope_status = -ENOMEM;
  137. return NULL;
  138. }
  139. }
  140. info->event = event;
  141. info->dev = dev;
  142. info->seg = pci_domain_nr(dev->bus);
  143. info->level = level;
  144. if (event == BUS_NOTIFY_ADD_DEVICE) {
  145. for (tmp = dev; tmp; tmp = tmp->bus->self) {
  146. level--;
  147. info->path[level].bus = tmp->bus->number;
  148. info->path[level].device = PCI_SLOT(tmp->devfn);
  149. info->path[level].function = PCI_FUNC(tmp->devfn);
  150. if (pci_is_root_bus(tmp->bus))
  151. info->bus = tmp->bus->number;
  152. }
  153. }
  154. return info;
  155. }
  156. static inline void dmar_free_pci_notify_info(struct dmar_pci_notify_info *info)
  157. {
  158. if ((void *)info != dmar_pci_notify_info_buf)
  159. kfree(info);
  160. }
  161. static bool dmar_match_pci_path(struct dmar_pci_notify_info *info, int bus,
  162. struct acpi_dmar_pci_path *path, int count)
  163. {
  164. int i;
  165. if (info->bus != bus)
  166. goto fallback;
  167. if (info->level != count)
  168. goto fallback;
  169. for (i = 0; i < count; i++) {
  170. if (path[i].device != info->path[i].device ||
  171. path[i].function != info->path[i].function)
  172. goto fallback;
  173. }
  174. return true;
  175. fallback:
  176. if (count != 1)
  177. return false;
  178. i = info->level - 1;
  179. if (bus == info->path[i].bus &&
  180. path[0].device == info->path[i].device &&
  181. path[0].function == info->path[i].function) {
  182. pr_info(FW_BUG "RMRR entry for device %02x:%02x.%x is broken - applying workaround\n",
  183. bus, path[0].device, path[0].function);
  184. return true;
  185. }
  186. return false;
  187. }
  188. /* Return: > 0 if match found, 0 if no match found, < 0 if error happens */
  189. int dmar_insert_dev_scope(struct dmar_pci_notify_info *info,
  190. void *start, void*end, u16 segment,
  191. struct dmar_dev_scope *devices,
  192. int devices_cnt)
  193. {
  194. int i, level;
  195. struct device *tmp, *dev = &info->dev->dev;
  196. struct acpi_dmar_device_scope *scope;
  197. struct acpi_dmar_pci_path *path;
  198. if (segment != info->seg)
  199. return 0;
  200. for (; start < end; start += scope->length) {
  201. scope = start;
  202. if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_ENDPOINT &&
  203. scope->entry_type != ACPI_DMAR_SCOPE_TYPE_BRIDGE)
  204. continue;
  205. path = (struct acpi_dmar_pci_path *)(scope + 1);
  206. level = (scope->length - sizeof(*scope)) / sizeof(*path);
  207. if (!dmar_match_pci_path(info, scope->bus, path, level))
  208. continue;
  209. if ((scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT) ^
  210. (info->dev->hdr_type == PCI_HEADER_TYPE_NORMAL)) {
  211. pr_warn("Device scope type does not match for %s\n",
  212. pci_name(info->dev));
  213. return -EINVAL;
  214. }
  215. for_each_dev_scope(devices, devices_cnt, i, tmp)
  216. if (tmp == NULL) {
  217. devices[i].bus = info->dev->bus->number;
  218. devices[i].devfn = info->dev->devfn;
  219. rcu_assign_pointer(devices[i].dev,
  220. get_device(dev));
  221. return 1;
  222. }
  223. BUG_ON(i >= devices_cnt);
  224. }
  225. return 0;
  226. }
  227. int dmar_remove_dev_scope(struct dmar_pci_notify_info *info, u16 segment,
  228. struct dmar_dev_scope *devices, int count)
  229. {
  230. int index;
  231. struct device *tmp;
  232. if (info->seg != segment)
  233. return 0;
  234. for_each_active_dev_scope(devices, count, index, tmp)
  235. if (tmp == &info->dev->dev) {
  236. RCU_INIT_POINTER(devices[index].dev, NULL);
  237. synchronize_rcu();
  238. put_device(tmp);
  239. return 1;
  240. }
  241. return 0;
  242. }
  243. static int dmar_pci_bus_add_dev(struct dmar_pci_notify_info *info)
  244. {
  245. int ret = 0;
  246. struct dmar_drhd_unit *dmaru;
  247. struct acpi_dmar_hardware_unit *drhd;
  248. for_each_drhd_unit(dmaru) {
  249. if (dmaru->include_all)
  250. continue;
  251. drhd = container_of(dmaru->hdr,
  252. struct acpi_dmar_hardware_unit, header);
  253. ret = dmar_insert_dev_scope(info, (void *)(drhd + 1),
  254. ((void *)drhd) + drhd->header.length,
  255. dmaru->segment,
  256. dmaru->devices, dmaru->devices_cnt);
  257. if (ret != 0)
  258. break;
  259. }
  260. if (ret >= 0)
  261. ret = dmar_iommu_notify_scope_dev(info);
  262. if (ret < 0 && dmar_dev_scope_status == 0)
  263. dmar_dev_scope_status = ret;
  264. return ret;
  265. }
  266. static void dmar_pci_bus_del_dev(struct dmar_pci_notify_info *info)
  267. {
  268. struct dmar_drhd_unit *dmaru;
  269. for_each_drhd_unit(dmaru)
  270. if (dmar_remove_dev_scope(info, dmaru->segment,
  271. dmaru->devices, dmaru->devices_cnt))
  272. break;
  273. dmar_iommu_notify_scope_dev(info);
  274. }
  275. static int dmar_pci_bus_notifier(struct notifier_block *nb,
  276. unsigned long action, void *data)
  277. {
  278. struct pci_dev *pdev = to_pci_dev(data);
  279. struct dmar_pci_notify_info *info;
  280. /* Only care about add/remove events for physical functions.
  281. * For VFs we actually do the lookup based on the corresponding
  282. * PF in device_to_iommu() anyway. */
  283. if (pdev->is_virtfn)
  284. return NOTIFY_DONE;
  285. if (action != BUS_NOTIFY_ADD_DEVICE &&
  286. action != BUS_NOTIFY_REMOVED_DEVICE)
  287. return NOTIFY_DONE;
  288. info = dmar_alloc_pci_notify_info(pdev, action);
  289. if (!info)
  290. return NOTIFY_DONE;
  291. down_write(&dmar_global_lock);
  292. if (action == BUS_NOTIFY_ADD_DEVICE)
  293. dmar_pci_bus_add_dev(info);
  294. else if (action == BUS_NOTIFY_REMOVED_DEVICE)
  295. dmar_pci_bus_del_dev(info);
  296. up_write(&dmar_global_lock);
  297. dmar_free_pci_notify_info(info);
  298. return NOTIFY_OK;
  299. }
  300. static struct notifier_block dmar_pci_bus_nb = {
  301. .notifier_call = dmar_pci_bus_notifier,
  302. .priority = INT_MIN,
  303. };
  304. static struct dmar_drhd_unit *
  305. dmar_find_dmaru(struct acpi_dmar_hardware_unit *drhd)
  306. {
  307. struct dmar_drhd_unit *dmaru;
  308. list_for_each_entry_rcu(dmaru, &dmar_drhd_units, list)
  309. if (dmaru->segment == drhd->segment &&
  310. dmaru->reg_base_addr == drhd->address)
  311. return dmaru;
  312. return NULL;
  313. }
  314. /**
  315. * dmar_parse_one_drhd - parses exactly one DMA remapping hardware definition
  316. * structure which uniquely represent one DMA remapping hardware unit
  317. * present in the platform
  318. */
  319. static int dmar_parse_one_drhd(struct acpi_dmar_header *header, void *arg)
  320. {
  321. struct acpi_dmar_hardware_unit *drhd;
  322. struct dmar_drhd_unit *dmaru;
  323. int ret = 0;
  324. drhd = (struct acpi_dmar_hardware_unit *)header;
  325. dmaru = dmar_find_dmaru(drhd);
  326. if (dmaru)
  327. goto out;
  328. dmaru = kzalloc(sizeof(*dmaru) + header->length, GFP_KERNEL);
  329. if (!dmaru)
  330. return -ENOMEM;
  331. /*
  332. * If header is allocated from slab by ACPI _DSM method, we need to
  333. * copy the content because the memory buffer will be freed on return.
  334. */
  335. dmaru->hdr = (void *)(dmaru + 1);
  336. memcpy(dmaru->hdr, header, header->length);
  337. dmaru->reg_base_addr = drhd->address;
  338. dmaru->segment = drhd->segment;
  339. dmaru->include_all = drhd->flags & 0x1; /* BIT0: INCLUDE_ALL */
  340. dmaru->devices = dmar_alloc_dev_scope((void *)(drhd + 1),
  341. ((void *)drhd) + drhd->header.length,
  342. &dmaru->devices_cnt);
  343. if (dmaru->devices_cnt && dmaru->devices == NULL) {
  344. kfree(dmaru);
  345. return -ENOMEM;
  346. }
  347. ret = alloc_iommu(dmaru);
  348. if (ret) {
  349. dmar_free_dev_scope(&dmaru->devices,
  350. &dmaru->devices_cnt);
  351. kfree(dmaru);
  352. return ret;
  353. }
  354. dmar_register_drhd_unit(dmaru);
  355. out:
  356. if (arg)
  357. (*(int *)arg)++;
  358. return 0;
  359. }
  360. static void dmar_free_drhd(struct dmar_drhd_unit *dmaru)
  361. {
  362. if (dmaru->devices && dmaru->devices_cnt)
  363. dmar_free_dev_scope(&dmaru->devices, &dmaru->devices_cnt);
  364. if (dmaru->iommu)
  365. free_iommu(dmaru->iommu);
  366. kfree(dmaru);
  367. }
  368. static int __init dmar_parse_one_andd(struct acpi_dmar_header *header,
  369. void *arg)
  370. {
  371. struct acpi_dmar_andd *andd = (void *)header;
  372. /* Check for NUL termination within the designated length */
  373. if (strnlen(andd->device_name, header->length - 8) == header->length - 8) {
  374. WARN_TAINT(1, TAINT_FIRMWARE_WORKAROUND,
  375. "Your BIOS is broken; ANDD object name is not NUL-terminated\n"
  376. "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
  377. dmi_get_system_info(DMI_BIOS_VENDOR),
  378. dmi_get_system_info(DMI_BIOS_VERSION),
  379. dmi_get_system_info(DMI_PRODUCT_VERSION));
  380. return -EINVAL;
  381. }
  382. pr_info("ANDD device: %x name: %s\n", andd->device_number,
  383. andd->device_name);
  384. return 0;
  385. }
  386. #ifdef CONFIG_ACPI_NUMA
  387. static int dmar_parse_one_rhsa(struct acpi_dmar_header *header, void *arg)
  388. {
  389. struct acpi_dmar_rhsa *rhsa;
  390. struct dmar_drhd_unit *drhd;
  391. rhsa = (struct acpi_dmar_rhsa *)header;
  392. for_each_drhd_unit(drhd) {
  393. if (drhd->reg_base_addr == rhsa->base_address) {
  394. int node = acpi_map_pxm_to_node(rhsa->proximity_domain);
  395. if (!node_online(node))
  396. node = -1;
  397. drhd->iommu->node = node;
  398. return 0;
  399. }
  400. }
  401. WARN_TAINT(
  402. 1, TAINT_FIRMWARE_WORKAROUND,
  403. "Your BIOS is broken; RHSA refers to non-existent DMAR unit at %llx\n"
  404. "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
  405. drhd->reg_base_addr,
  406. dmi_get_system_info(DMI_BIOS_VENDOR),
  407. dmi_get_system_info(DMI_BIOS_VERSION),
  408. dmi_get_system_info(DMI_PRODUCT_VERSION));
  409. return 0;
  410. }
  411. #else
  412. #define dmar_parse_one_rhsa dmar_res_noop
  413. #endif
  414. static void __init
  415. dmar_table_print_dmar_entry(struct acpi_dmar_header *header)
  416. {
  417. struct acpi_dmar_hardware_unit *drhd;
  418. struct acpi_dmar_reserved_memory *rmrr;
  419. struct acpi_dmar_atsr *atsr;
  420. struct acpi_dmar_rhsa *rhsa;
  421. switch (header->type) {
  422. case ACPI_DMAR_TYPE_HARDWARE_UNIT:
  423. drhd = container_of(header, struct acpi_dmar_hardware_unit,
  424. header);
  425. pr_info("DRHD base: %#016Lx flags: %#x\n",
  426. (unsigned long long)drhd->address, drhd->flags);
  427. break;
  428. case ACPI_DMAR_TYPE_RESERVED_MEMORY:
  429. rmrr = container_of(header, struct acpi_dmar_reserved_memory,
  430. header);
  431. pr_info("RMRR base: %#016Lx end: %#016Lx\n",
  432. (unsigned long long)rmrr->base_address,
  433. (unsigned long long)rmrr->end_address);
  434. break;
  435. case ACPI_DMAR_TYPE_ROOT_ATS:
  436. atsr = container_of(header, struct acpi_dmar_atsr, header);
  437. pr_info("ATSR flags: %#x\n", atsr->flags);
  438. break;
  439. case ACPI_DMAR_TYPE_HARDWARE_AFFINITY:
  440. rhsa = container_of(header, struct acpi_dmar_rhsa, header);
  441. pr_info("RHSA base: %#016Lx proximity domain: %#x\n",
  442. (unsigned long long)rhsa->base_address,
  443. rhsa->proximity_domain);
  444. break;
  445. case ACPI_DMAR_TYPE_NAMESPACE:
  446. /* We don't print this here because we need to sanity-check
  447. it first. So print it in dmar_parse_one_andd() instead. */
  448. break;
  449. }
  450. }
  451. /**
  452. * dmar_table_detect - checks to see if the platform supports DMAR devices
  453. */
  454. static int __init dmar_table_detect(void)
  455. {
  456. acpi_status status = AE_OK;
  457. /* if we could find DMAR table, then there are DMAR devices */
  458. status = acpi_get_table_with_size(ACPI_SIG_DMAR, 0,
  459. (struct acpi_table_header **)&dmar_tbl,
  460. &dmar_tbl_size);
  461. if (ACPI_SUCCESS(status) && !dmar_tbl) {
  462. pr_warn("Unable to map DMAR\n");
  463. status = AE_NOT_FOUND;
  464. }
  465. return (ACPI_SUCCESS(status) ? 1 : 0);
  466. }
  467. static int dmar_walk_remapping_entries(struct acpi_dmar_header *start,
  468. size_t len, struct dmar_res_callback *cb)
  469. {
  470. int ret = 0;
  471. struct acpi_dmar_header *iter, *next;
  472. struct acpi_dmar_header *end = ((void *)start) + len;
  473. for (iter = start; iter < end && ret == 0; iter = next) {
  474. next = (void *)iter + iter->length;
  475. if (iter->length == 0) {
  476. /* Avoid looping forever on bad ACPI tables */
  477. pr_debug(FW_BUG "Invalid 0-length structure\n");
  478. break;
  479. } else if (next > end) {
  480. /* Avoid passing table end */
  481. pr_warn(FW_BUG "Record passes table end\n");
  482. ret = -EINVAL;
  483. break;
  484. }
  485. if (cb->print_entry)
  486. dmar_table_print_dmar_entry(iter);
  487. if (iter->type >= ACPI_DMAR_TYPE_RESERVED) {
  488. /* continue for forward compatibility */
  489. pr_debug("Unknown DMAR structure type %d\n",
  490. iter->type);
  491. } else if (cb->cb[iter->type]) {
  492. ret = cb->cb[iter->type](iter, cb->arg[iter->type]);
  493. } else if (!cb->ignore_unhandled) {
  494. pr_warn("No handler for DMAR structure type %d\n",
  495. iter->type);
  496. ret = -EINVAL;
  497. }
  498. }
  499. return ret;
  500. }
  501. static inline int dmar_walk_dmar_table(struct acpi_table_dmar *dmar,
  502. struct dmar_res_callback *cb)
  503. {
  504. return dmar_walk_remapping_entries((void *)(dmar + 1),
  505. dmar->header.length - sizeof(*dmar), cb);
  506. }
  507. /**
  508. * parse_dmar_table - parses the DMA reporting table
  509. */
  510. static int __init
  511. parse_dmar_table(void)
  512. {
  513. struct acpi_table_dmar *dmar;
  514. int ret = 0;
  515. int drhd_count = 0;
  516. struct dmar_res_callback cb = {
  517. .print_entry = true,
  518. .ignore_unhandled = true,
  519. .arg[ACPI_DMAR_TYPE_HARDWARE_UNIT] = &drhd_count,
  520. .cb[ACPI_DMAR_TYPE_HARDWARE_UNIT] = &dmar_parse_one_drhd,
  521. .cb[ACPI_DMAR_TYPE_RESERVED_MEMORY] = &dmar_parse_one_rmrr,
  522. .cb[ACPI_DMAR_TYPE_ROOT_ATS] = &dmar_parse_one_atsr,
  523. .cb[ACPI_DMAR_TYPE_HARDWARE_AFFINITY] = &dmar_parse_one_rhsa,
  524. .cb[ACPI_DMAR_TYPE_NAMESPACE] = &dmar_parse_one_andd,
  525. };
  526. /*
  527. * Do it again, earlier dmar_tbl mapping could be mapped with
  528. * fixed map.
  529. */
  530. dmar_table_detect();
  531. /*
  532. * ACPI tables may not be DMA protected by tboot, so use DMAR copy
  533. * SINIT saved in SinitMleData in TXT heap (which is DMA protected)
  534. */
  535. dmar_tbl = tboot_get_dmar_table(dmar_tbl);
  536. dmar = (struct acpi_table_dmar *)dmar_tbl;
  537. if (!dmar)
  538. return -ENODEV;
  539. if (dmar->width < PAGE_SHIFT - 1) {
  540. pr_warn("Invalid DMAR haw\n");
  541. return -EINVAL;
  542. }
  543. pr_info("Host address width %d\n", dmar->width + 1);
  544. ret = dmar_walk_dmar_table(dmar, &cb);
  545. if (ret == 0 && drhd_count == 0)
  546. pr_warn(FW_BUG "No DRHD structure found in DMAR table\n");
  547. return ret;
  548. }
  549. static int dmar_pci_device_match(struct dmar_dev_scope devices[],
  550. int cnt, struct pci_dev *dev)
  551. {
  552. int index;
  553. struct device *tmp;
  554. while (dev) {
  555. for_each_active_dev_scope(devices, cnt, index, tmp)
  556. if (dev_is_pci(tmp) && dev == to_pci_dev(tmp))
  557. return 1;
  558. /* Check our parent */
  559. dev = dev->bus->self;
  560. }
  561. return 0;
  562. }
  563. struct dmar_drhd_unit *
  564. dmar_find_matched_drhd_unit(struct pci_dev *dev)
  565. {
  566. struct dmar_drhd_unit *dmaru;
  567. struct acpi_dmar_hardware_unit *drhd;
  568. dev = pci_physfn(dev);
  569. rcu_read_lock();
  570. for_each_drhd_unit(dmaru) {
  571. drhd = container_of(dmaru->hdr,
  572. struct acpi_dmar_hardware_unit,
  573. header);
  574. if (dmaru->include_all &&
  575. drhd->segment == pci_domain_nr(dev->bus))
  576. goto out;
  577. if (dmar_pci_device_match(dmaru->devices,
  578. dmaru->devices_cnt, dev))
  579. goto out;
  580. }
  581. dmaru = NULL;
  582. out:
  583. rcu_read_unlock();
  584. return dmaru;
  585. }
  586. static void __init dmar_acpi_insert_dev_scope(u8 device_number,
  587. struct acpi_device *adev)
  588. {
  589. struct dmar_drhd_unit *dmaru;
  590. struct acpi_dmar_hardware_unit *drhd;
  591. struct acpi_dmar_device_scope *scope;
  592. struct device *tmp;
  593. int i;
  594. struct acpi_dmar_pci_path *path;
  595. for_each_drhd_unit(dmaru) {
  596. drhd = container_of(dmaru->hdr,
  597. struct acpi_dmar_hardware_unit,
  598. header);
  599. for (scope = (void *)(drhd + 1);
  600. (unsigned long)scope < ((unsigned long)drhd) + drhd->header.length;
  601. scope = ((void *)scope) + scope->length) {
  602. if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_NAMESPACE)
  603. continue;
  604. if (scope->enumeration_id != device_number)
  605. continue;
  606. path = (void *)(scope + 1);
  607. pr_info("ACPI device \"%s\" under DMAR at %llx as %02x:%02x.%d\n",
  608. dev_name(&adev->dev), dmaru->reg_base_addr,
  609. scope->bus, path->device, path->function);
  610. for_each_dev_scope(dmaru->devices, dmaru->devices_cnt, i, tmp)
  611. if (tmp == NULL) {
  612. dmaru->devices[i].bus = scope->bus;
  613. dmaru->devices[i].devfn = PCI_DEVFN(path->device,
  614. path->function);
  615. rcu_assign_pointer(dmaru->devices[i].dev,
  616. get_device(&adev->dev));
  617. return;
  618. }
  619. BUG_ON(i >= dmaru->devices_cnt);
  620. }
  621. }
  622. pr_warn("No IOMMU scope found for ANDD enumeration ID %d (%s)\n",
  623. device_number, dev_name(&adev->dev));
  624. }
  625. static int __init dmar_acpi_dev_scope_init(void)
  626. {
  627. struct acpi_dmar_andd *andd;
  628. if (dmar_tbl == NULL)
  629. return -ENODEV;
  630. for (andd = (void *)dmar_tbl + sizeof(struct acpi_table_dmar);
  631. ((unsigned long)andd) < ((unsigned long)dmar_tbl) + dmar_tbl->length;
  632. andd = ((void *)andd) + andd->header.length) {
  633. if (andd->header.type == ACPI_DMAR_TYPE_NAMESPACE) {
  634. acpi_handle h;
  635. struct acpi_device *adev;
  636. if (!ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT,
  637. andd->device_name,
  638. &h))) {
  639. pr_err("Failed to find handle for ACPI object %s\n",
  640. andd->device_name);
  641. continue;
  642. }
  643. if (acpi_bus_get_device(h, &adev)) {
  644. pr_err("Failed to get device for ACPI object %s\n",
  645. andd->device_name);
  646. continue;
  647. }
  648. dmar_acpi_insert_dev_scope(andd->device_number, adev);
  649. }
  650. }
  651. return 0;
  652. }
  653. int __init dmar_dev_scope_init(void)
  654. {
  655. struct pci_dev *dev = NULL;
  656. struct dmar_pci_notify_info *info;
  657. if (dmar_dev_scope_status != 1)
  658. return dmar_dev_scope_status;
  659. if (list_empty(&dmar_drhd_units)) {
  660. dmar_dev_scope_status = -ENODEV;
  661. } else {
  662. dmar_dev_scope_status = 0;
  663. dmar_acpi_dev_scope_init();
  664. for_each_pci_dev(dev) {
  665. if (dev->is_virtfn)
  666. continue;
  667. info = dmar_alloc_pci_notify_info(dev,
  668. BUS_NOTIFY_ADD_DEVICE);
  669. if (!info) {
  670. return dmar_dev_scope_status;
  671. } else {
  672. dmar_pci_bus_add_dev(info);
  673. dmar_free_pci_notify_info(info);
  674. }
  675. }
  676. bus_register_notifier(&pci_bus_type, &dmar_pci_bus_nb);
  677. }
  678. return dmar_dev_scope_status;
  679. }
  680. int __init dmar_table_init(void)
  681. {
  682. static int dmar_table_initialized;
  683. int ret;
  684. if (dmar_table_initialized == 0) {
  685. ret = parse_dmar_table();
  686. if (ret < 0) {
  687. if (ret != -ENODEV)
  688. pr_info("Parse DMAR table failure.\n");
  689. } else if (list_empty(&dmar_drhd_units)) {
  690. pr_info("No DMAR devices found\n");
  691. ret = -ENODEV;
  692. }
  693. if (ret < 0)
  694. dmar_table_initialized = ret;
  695. else
  696. dmar_table_initialized = 1;
  697. }
  698. return dmar_table_initialized < 0 ? dmar_table_initialized : 0;
  699. }
  700. static void warn_invalid_dmar(u64 addr, const char *message)
  701. {
  702. WARN_TAINT_ONCE(
  703. 1, TAINT_FIRMWARE_WORKAROUND,
  704. "Your BIOS is broken; DMAR reported at address %llx%s!\n"
  705. "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
  706. addr, message,
  707. dmi_get_system_info(DMI_BIOS_VENDOR),
  708. dmi_get_system_info(DMI_BIOS_VERSION),
  709. dmi_get_system_info(DMI_PRODUCT_VERSION));
  710. }
  711. static int __ref
  712. dmar_validate_one_drhd(struct acpi_dmar_header *entry, void *arg)
  713. {
  714. struct acpi_dmar_hardware_unit *drhd;
  715. void __iomem *addr;
  716. u64 cap, ecap;
  717. drhd = (void *)entry;
  718. if (!drhd->address) {
  719. warn_invalid_dmar(0, "");
  720. return -EINVAL;
  721. }
  722. if (arg)
  723. addr = ioremap(drhd->address, VTD_PAGE_SIZE);
  724. else
  725. addr = early_ioremap(drhd->address, VTD_PAGE_SIZE);
  726. if (!addr) {
  727. pr_warn("Can't validate DRHD address: %llx\n", drhd->address);
  728. return -EINVAL;
  729. }
  730. cap = dmar_readq(addr + DMAR_CAP_REG);
  731. ecap = dmar_readq(addr + DMAR_ECAP_REG);
  732. if (arg)
  733. iounmap(addr);
  734. else
  735. early_iounmap(addr, VTD_PAGE_SIZE);
  736. if (cap == (uint64_t)-1 && ecap == (uint64_t)-1) {
  737. warn_invalid_dmar(drhd->address, " returns all ones");
  738. return -EINVAL;
  739. }
  740. return 0;
  741. }
  742. int __init detect_intel_iommu(void)
  743. {
  744. int ret;
  745. struct dmar_res_callback validate_drhd_cb = {
  746. .cb[ACPI_DMAR_TYPE_HARDWARE_UNIT] = &dmar_validate_one_drhd,
  747. .ignore_unhandled = true,
  748. };
  749. down_write(&dmar_global_lock);
  750. ret = dmar_table_detect();
  751. if (ret)
  752. ret = !dmar_walk_dmar_table((struct acpi_table_dmar *)dmar_tbl,
  753. &validate_drhd_cb);
  754. if (ret && !no_iommu && !iommu_detected && !dmar_disabled) {
  755. iommu_detected = 1;
  756. /* Make sure ACS will be enabled */
  757. pci_request_acs();
  758. }
  759. #ifdef CONFIG_X86
  760. if (ret)
  761. x86_init.iommu.iommu_init = intel_iommu_init;
  762. #endif
  763. early_acpi_os_unmap_memory((void __iomem *)dmar_tbl, dmar_tbl_size);
  764. dmar_tbl = NULL;
  765. up_write(&dmar_global_lock);
  766. return ret ? 1 : -ENODEV;
  767. }
  768. static void unmap_iommu(struct intel_iommu *iommu)
  769. {
  770. iounmap(iommu->reg);
  771. release_mem_region(iommu->reg_phys, iommu->reg_size);
  772. }
  773. /**
  774. * map_iommu: map the iommu's registers
  775. * @iommu: the iommu to map
  776. * @phys_addr: the physical address of the base resgister
  777. *
  778. * Memory map the iommu's registers. Start w/ a single page, and
  779. * possibly expand if that turns out to be insufficent.
  780. */
  781. static int map_iommu(struct intel_iommu *iommu, u64 phys_addr)
  782. {
  783. int map_size, err=0;
  784. iommu->reg_phys = phys_addr;
  785. iommu->reg_size = VTD_PAGE_SIZE;
  786. if (!request_mem_region(iommu->reg_phys, iommu->reg_size, iommu->name)) {
  787. pr_err("Can't reserve memory\n");
  788. err = -EBUSY;
  789. goto out;
  790. }
  791. iommu->reg = ioremap(iommu->reg_phys, iommu->reg_size);
  792. if (!iommu->reg) {
  793. pr_err("Can't map the region\n");
  794. err = -ENOMEM;
  795. goto release;
  796. }
  797. iommu->cap = dmar_readq(iommu->reg + DMAR_CAP_REG);
  798. iommu->ecap = dmar_readq(iommu->reg + DMAR_ECAP_REG);
  799. if (iommu->cap == (uint64_t)-1 && iommu->ecap == (uint64_t)-1) {
  800. err = -EINVAL;
  801. warn_invalid_dmar(phys_addr, " returns all ones");
  802. goto unmap;
  803. }
  804. /* the registers might be more than one page */
  805. map_size = max_t(int, ecap_max_iotlb_offset(iommu->ecap),
  806. cap_max_fault_reg_offset(iommu->cap));
  807. map_size = VTD_PAGE_ALIGN(map_size);
  808. if (map_size > iommu->reg_size) {
  809. iounmap(iommu->reg);
  810. release_mem_region(iommu->reg_phys, iommu->reg_size);
  811. iommu->reg_size = map_size;
  812. if (!request_mem_region(iommu->reg_phys, iommu->reg_size,
  813. iommu->name)) {
  814. pr_err("Can't reserve memory\n");
  815. err = -EBUSY;
  816. goto out;
  817. }
  818. iommu->reg = ioremap(iommu->reg_phys, iommu->reg_size);
  819. if (!iommu->reg) {
  820. pr_err("Can't map the region\n");
  821. err = -ENOMEM;
  822. goto release;
  823. }
  824. }
  825. err = 0;
  826. goto out;
  827. unmap:
  828. iounmap(iommu->reg);
  829. release:
  830. release_mem_region(iommu->reg_phys, iommu->reg_size);
  831. out:
  832. return err;
  833. }
  834. static int dmar_alloc_seq_id(struct intel_iommu *iommu)
  835. {
  836. iommu->seq_id = find_first_zero_bit(dmar_seq_ids,
  837. DMAR_UNITS_SUPPORTED);
  838. if (iommu->seq_id >= DMAR_UNITS_SUPPORTED) {
  839. iommu->seq_id = -1;
  840. } else {
  841. set_bit(iommu->seq_id, dmar_seq_ids);
  842. sprintf(iommu->name, "dmar%d", iommu->seq_id);
  843. }
  844. return iommu->seq_id;
  845. }
  846. static void dmar_free_seq_id(struct intel_iommu *iommu)
  847. {
  848. if (iommu->seq_id >= 0) {
  849. clear_bit(iommu->seq_id, dmar_seq_ids);
  850. iommu->seq_id = -1;
  851. }
  852. }
  853. static int alloc_iommu(struct dmar_drhd_unit *drhd)
  854. {
  855. struct intel_iommu *iommu;
  856. u32 ver, sts;
  857. int agaw = 0;
  858. int msagaw = 0;
  859. int err;
  860. if (!drhd->reg_base_addr) {
  861. warn_invalid_dmar(0, "");
  862. return -EINVAL;
  863. }
  864. iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
  865. if (!iommu)
  866. return -ENOMEM;
  867. if (dmar_alloc_seq_id(iommu) < 0) {
  868. pr_err("Failed to allocate seq_id\n");
  869. err = -ENOSPC;
  870. goto error;
  871. }
  872. err = map_iommu(iommu, drhd->reg_base_addr);
  873. if (err) {
  874. pr_err("Failed to map %s\n", iommu->name);
  875. goto error_free_seq_id;
  876. }
  877. err = -EINVAL;
  878. agaw = iommu_calculate_agaw(iommu);
  879. if (agaw < 0) {
  880. pr_err("Cannot get a valid agaw for iommu (seq_id = %d)\n",
  881. iommu->seq_id);
  882. goto err_unmap;
  883. }
  884. msagaw = iommu_calculate_max_sagaw(iommu);
  885. if (msagaw < 0) {
  886. pr_err("Cannot get a valid max agaw for iommu (seq_id = %d)\n",
  887. iommu->seq_id);
  888. goto err_unmap;
  889. }
  890. iommu->agaw = agaw;
  891. iommu->msagaw = msagaw;
  892. iommu->segment = drhd->segment;
  893. iommu->node = -1;
  894. ver = readl(iommu->reg + DMAR_VER_REG);
  895. pr_info("%s: reg_base_addr %llx ver %d:%d cap %llx ecap %llx\n",
  896. iommu->name,
  897. (unsigned long long)drhd->reg_base_addr,
  898. DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver),
  899. (unsigned long long)iommu->cap,
  900. (unsigned long long)iommu->ecap);
  901. /* Reflect status in gcmd */
  902. sts = readl(iommu->reg + DMAR_GSTS_REG);
  903. if (sts & DMA_GSTS_IRES)
  904. iommu->gcmd |= DMA_GCMD_IRE;
  905. if (sts & DMA_GSTS_TES)
  906. iommu->gcmd |= DMA_GCMD_TE;
  907. if (sts & DMA_GSTS_QIES)
  908. iommu->gcmd |= DMA_GCMD_QIE;
  909. raw_spin_lock_init(&iommu->register_lock);
  910. drhd->iommu = iommu;
  911. if (intel_iommu_enabled)
  912. iommu->iommu_dev = iommu_device_create(NULL, iommu,
  913. intel_iommu_groups,
  914. "%s", iommu->name);
  915. return 0;
  916. err_unmap:
  917. unmap_iommu(iommu);
  918. error_free_seq_id:
  919. dmar_free_seq_id(iommu);
  920. error:
  921. kfree(iommu);
  922. return err;
  923. }
  924. static void free_iommu(struct intel_iommu *iommu)
  925. {
  926. iommu_device_destroy(iommu->iommu_dev);
  927. if (iommu->irq) {
  928. if (iommu->pr_irq) {
  929. free_irq(iommu->pr_irq, iommu);
  930. dmar_free_hwirq(iommu->pr_irq);
  931. iommu->pr_irq = 0;
  932. }
  933. free_irq(iommu->irq, iommu);
  934. dmar_free_hwirq(iommu->irq);
  935. iommu->irq = 0;
  936. }
  937. if (iommu->qi) {
  938. free_page((unsigned long)iommu->qi->desc);
  939. kfree(iommu->qi->desc_status);
  940. kfree(iommu->qi);
  941. }
  942. if (iommu->reg)
  943. unmap_iommu(iommu);
  944. dmar_free_seq_id(iommu);
  945. kfree(iommu);
  946. }
  947. /*
  948. * Reclaim all the submitted descriptors which have completed its work.
  949. */
  950. static inline void reclaim_free_desc(struct q_inval *qi)
  951. {
  952. while (qi->desc_status[qi->free_tail] == QI_DONE ||
  953. qi->desc_status[qi->free_tail] == QI_ABORT) {
  954. qi->desc_status[qi->free_tail] = QI_FREE;
  955. qi->free_tail = (qi->free_tail + 1) % QI_LENGTH;
  956. qi->free_cnt++;
  957. }
  958. }
  959. static int qi_check_fault(struct intel_iommu *iommu, int index)
  960. {
  961. u32 fault;
  962. int head, tail;
  963. struct q_inval *qi = iommu->qi;
  964. int wait_index = (index + 1) % QI_LENGTH;
  965. if (qi->desc_status[wait_index] == QI_ABORT)
  966. return -EAGAIN;
  967. fault = readl(iommu->reg + DMAR_FSTS_REG);
  968. /*
  969. * If IQE happens, the head points to the descriptor associated
  970. * with the error. No new descriptors are fetched until the IQE
  971. * is cleared.
  972. */
  973. if (fault & DMA_FSTS_IQE) {
  974. head = readl(iommu->reg + DMAR_IQH_REG);
  975. if ((head >> DMAR_IQ_SHIFT) == index) {
  976. pr_err("VT-d detected invalid descriptor: "
  977. "low=%llx, high=%llx\n",
  978. (unsigned long long)qi->desc[index].low,
  979. (unsigned long long)qi->desc[index].high);
  980. memcpy(&qi->desc[index], &qi->desc[wait_index],
  981. sizeof(struct qi_desc));
  982. __iommu_flush_cache(iommu, &qi->desc[index],
  983. sizeof(struct qi_desc));
  984. writel(DMA_FSTS_IQE, iommu->reg + DMAR_FSTS_REG);
  985. return -EINVAL;
  986. }
  987. }
  988. /*
  989. * If ITE happens, all pending wait_desc commands are aborted.
  990. * No new descriptors are fetched until the ITE is cleared.
  991. */
  992. if (fault & DMA_FSTS_ITE) {
  993. head = readl(iommu->reg + DMAR_IQH_REG);
  994. head = ((head >> DMAR_IQ_SHIFT) - 1 + QI_LENGTH) % QI_LENGTH;
  995. head |= 1;
  996. tail = readl(iommu->reg + DMAR_IQT_REG);
  997. tail = ((tail >> DMAR_IQ_SHIFT) - 1 + QI_LENGTH) % QI_LENGTH;
  998. writel(DMA_FSTS_ITE, iommu->reg + DMAR_FSTS_REG);
  999. do {
  1000. if (qi->desc_status[head] == QI_IN_USE)
  1001. qi->desc_status[head] = QI_ABORT;
  1002. head = (head - 2 + QI_LENGTH) % QI_LENGTH;
  1003. } while (head != tail);
  1004. if (qi->desc_status[wait_index] == QI_ABORT)
  1005. return -EAGAIN;
  1006. }
  1007. if (fault & DMA_FSTS_ICE)
  1008. writel(DMA_FSTS_ICE, iommu->reg + DMAR_FSTS_REG);
  1009. return 0;
  1010. }
  1011. /*
  1012. * Submit the queued invalidation descriptor to the remapping
  1013. * hardware unit and wait for its completion.
  1014. */
  1015. int qi_submit_sync(struct qi_desc *desc, struct intel_iommu *iommu)
  1016. {
  1017. int rc;
  1018. struct q_inval *qi = iommu->qi;
  1019. struct qi_desc *hw, wait_desc;
  1020. int wait_index, index;
  1021. unsigned long flags;
  1022. if (!qi)
  1023. return 0;
  1024. hw = qi->desc;
  1025. restart:
  1026. rc = 0;
  1027. raw_spin_lock_irqsave(&qi->q_lock, flags);
  1028. while (qi->free_cnt < 3) {
  1029. raw_spin_unlock_irqrestore(&qi->q_lock, flags);
  1030. cpu_relax();
  1031. raw_spin_lock_irqsave(&qi->q_lock, flags);
  1032. }
  1033. index = qi->free_head;
  1034. wait_index = (index + 1) % QI_LENGTH;
  1035. qi->desc_status[index] = qi->desc_status[wait_index] = QI_IN_USE;
  1036. hw[index] = *desc;
  1037. wait_desc.low = QI_IWD_STATUS_DATA(QI_DONE) |
  1038. QI_IWD_STATUS_WRITE | QI_IWD_TYPE;
  1039. wait_desc.high = virt_to_phys(&qi->desc_status[wait_index]);
  1040. hw[wait_index] = wait_desc;
  1041. __iommu_flush_cache(iommu, &hw[index], sizeof(struct qi_desc));
  1042. __iommu_flush_cache(iommu, &hw[wait_index], sizeof(struct qi_desc));
  1043. qi->free_head = (qi->free_head + 2) % QI_LENGTH;
  1044. qi->free_cnt -= 2;
  1045. /*
  1046. * update the HW tail register indicating the presence of
  1047. * new descriptors.
  1048. */
  1049. writel(qi->free_head << DMAR_IQ_SHIFT, iommu->reg + DMAR_IQT_REG);
  1050. while (qi->desc_status[wait_index] != QI_DONE) {
  1051. /*
  1052. * We will leave the interrupts disabled, to prevent interrupt
  1053. * context to queue another cmd while a cmd is already submitted
  1054. * and waiting for completion on this cpu. This is to avoid
  1055. * a deadlock where the interrupt context can wait indefinitely
  1056. * for free slots in the queue.
  1057. */
  1058. rc = qi_check_fault(iommu, index);
  1059. if (rc)
  1060. break;
  1061. raw_spin_unlock(&qi->q_lock);
  1062. cpu_relax();
  1063. raw_spin_lock(&qi->q_lock);
  1064. }
  1065. qi->desc_status[index] = QI_DONE;
  1066. reclaim_free_desc(qi);
  1067. raw_spin_unlock_irqrestore(&qi->q_lock, flags);
  1068. if (rc == -EAGAIN)
  1069. goto restart;
  1070. return rc;
  1071. }
  1072. /*
  1073. * Flush the global interrupt entry cache.
  1074. */
  1075. void qi_global_iec(struct intel_iommu *iommu)
  1076. {
  1077. struct qi_desc desc;
  1078. desc.low = QI_IEC_TYPE;
  1079. desc.high = 0;
  1080. /* should never fail */
  1081. qi_submit_sync(&desc, iommu);
  1082. }
  1083. void qi_flush_context(struct intel_iommu *iommu, u16 did, u16 sid, u8 fm,
  1084. u64 type)
  1085. {
  1086. struct qi_desc desc;
  1087. desc.low = QI_CC_FM(fm) | QI_CC_SID(sid) | QI_CC_DID(did)
  1088. | QI_CC_GRAN(type) | QI_CC_TYPE;
  1089. desc.high = 0;
  1090. qi_submit_sync(&desc, iommu);
  1091. }
  1092. void qi_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,
  1093. unsigned int size_order, u64 type)
  1094. {
  1095. u8 dw = 0, dr = 0;
  1096. struct qi_desc desc;
  1097. int ih = 0;
  1098. if (cap_write_drain(iommu->cap))
  1099. dw = 1;
  1100. if (cap_read_drain(iommu->cap))
  1101. dr = 1;
  1102. desc.low = QI_IOTLB_DID(did) | QI_IOTLB_DR(dr) | QI_IOTLB_DW(dw)
  1103. | QI_IOTLB_GRAN(type) | QI_IOTLB_TYPE;
  1104. desc.high = QI_IOTLB_ADDR(addr) | QI_IOTLB_IH(ih)
  1105. | QI_IOTLB_AM(size_order);
  1106. qi_submit_sync(&desc, iommu);
  1107. }
  1108. void qi_flush_dev_iotlb(struct intel_iommu *iommu, u16 sid, u16 pfsid,
  1109. u16 qdep, u64 addr, unsigned mask)
  1110. {
  1111. struct qi_desc desc;
  1112. if (mask) {
  1113. BUG_ON(addr & ((1 << (VTD_PAGE_SHIFT + mask)) - 1));
  1114. addr |= (1 << (VTD_PAGE_SHIFT + mask - 1)) - 1;
  1115. desc.high = QI_DEV_IOTLB_ADDR(addr) | QI_DEV_IOTLB_SIZE;
  1116. } else
  1117. desc.high = QI_DEV_IOTLB_ADDR(addr);
  1118. if (qdep >= QI_DEV_IOTLB_MAX_INVS)
  1119. qdep = 0;
  1120. desc.low = QI_DEV_IOTLB_SID(sid) | QI_DEV_IOTLB_QDEP(qdep) |
  1121. QI_DIOTLB_TYPE | QI_DEV_IOTLB_PFSID(pfsid);
  1122. qi_submit_sync(&desc, iommu);
  1123. }
  1124. /*
  1125. * Disable Queued Invalidation interface.
  1126. */
  1127. void dmar_disable_qi(struct intel_iommu *iommu)
  1128. {
  1129. unsigned long flags;
  1130. u32 sts;
  1131. cycles_t start_time = get_cycles();
  1132. if (!ecap_qis(iommu->ecap))
  1133. return;
  1134. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  1135. sts = readl(iommu->reg + DMAR_GSTS_REG);
  1136. if (!(sts & DMA_GSTS_QIES))
  1137. goto end;
  1138. /*
  1139. * Give a chance to HW to complete the pending invalidation requests.
  1140. */
  1141. while ((readl(iommu->reg + DMAR_IQT_REG) !=
  1142. readl(iommu->reg + DMAR_IQH_REG)) &&
  1143. (DMAR_OPERATION_TIMEOUT > (get_cycles() - start_time)))
  1144. cpu_relax();
  1145. iommu->gcmd &= ~DMA_GCMD_QIE;
  1146. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  1147. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl,
  1148. !(sts & DMA_GSTS_QIES), sts);
  1149. end:
  1150. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  1151. }
  1152. /*
  1153. * Enable queued invalidation.
  1154. */
  1155. static void __dmar_enable_qi(struct intel_iommu *iommu)
  1156. {
  1157. u32 sts;
  1158. unsigned long flags;
  1159. struct q_inval *qi = iommu->qi;
  1160. qi->free_head = qi->free_tail = 0;
  1161. qi->free_cnt = QI_LENGTH;
  1162. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  1163. /* write zero to the tail reg */
  1164. writel(0, iommu->reg + DMAR_IQT_REG);
  1165. dmar_writeq(iommu->reg + DMAR_IQA_REG, virt_to_phys(qi->desc));
  1166. iommu->gcmd |= DMA_GCMD_QIE;
  1167. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  1168. /* Make sure hardware complete it */
  1169. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl, (sts & DMA_GSTS_QIES), sts);
  1170. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  1171. }
  1172. /*
  1173. * Enable Queued Invalidation interface. This is a must to support
  1174. * interrupt-remapping. Also used by DMA-remapping, which replaces
  1175. * register based IOTLB invalidation.
  1176. */
  1177. int dmar_enable_qi(struct intel_iommu *iommu)
  1178. {
  1179. struct q_inval *qi;
  1180. struct page *desc_page;
  1181. if (!ecap_qis(iommu->ecap))
  1182. return -ENOENT;
  1183. /*
  1184. * queued invalidation is already setup and enabled.
  1185. */
  1186. if (iommu->qi)
  1187. return 0;
  1188. iommu->qi = kmalloc(sizeof(*qi), GFP_ATOMIC);
  1189. if (!iommu->qi)
  1190. return -ENOMEM;
  1191. qi = iommu->qi;
  1192. desc_page = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO, 0);
  1193. if (!desc_page) {
  1194. kfree(qi);
  1195. iommu->qi = NULL;
  1196. return -ENOMEM;
  1197. }
  1198. qi->desc = page_address(desc_page);
  1199. qi->desc_status = kzalloc(QI_LENGTH * sizeof(int), GFP_ATOMIC);
  1200. if (!qi->desc_status) {
  1201. free_page((unsigned long) qi->desc);
  1202. kfree(qi);
  1203. iommu->qi = NULL;
  1204. return -ENOMEM;
  1205. }
  1206. raw_spin_lock_init(&qi->q_lock);
  1207. __dmar_enable_qi(iommu);
  1208. return 0;
  1209. }
  1210. /* iommu interrupt handling. Most stuff are MSI-like. */
  1211. enum faulttype {
  1212. DMA_REMAP,
  1213. INTR_REMAP,
  1214. UNKNOWN,
  1215. };
  1216. static const char *dma_remap_fault_reasons[] =
  1217. {
  1218. "Software",
  1219. "Present bit in root entry is clear",
  1220. "Present bit in context entry is clear",
  1221. "Invalid context entry",
  1222. "Access beyond MGAW",
  1223. "PTE Write access is not set",
  1224. "PTE Read access is not set",
  1225. "Next page table ptr is invalid",
  1226. "Root table address invalid",
  1227. "Context table ptr is invalid",
  1228. "non-zero reserved fields in RTP",
  1229. "non-zero reserved fields in CTP",
  1230. "non-zero reserved fields in PTE",
  1231. "PCE for translation request specifies blocking",
  1232. };
  1233. static const char *irq_remap_fault_reasons[] =
  1234. {
  1235. "Detected reserved fields in the decoded interrupt-remapped request",
  1236. "Interrupt index exceeded the interrupt-remapping table size",
  1237. "Present field in the IRTE entry is clear",
  1238. "Error accessing interrupt-remapping table pointed by IRTA_REG",
  1239. "Detected reserved fields in the IRTE entry",
  1240. "Blocked a compatibility format interrupt request",
  1241. "Blocked an interrupt request due to source-id verification failure",
  1242. };
  1243. static const char *dmar_get_fault_reason(u8 fault_reason, int *fault_type)
  1244. {
  1245. if (fault_reason >= 0x20 && (fault_reason - 0x20 <
  1246. ARRAY_SIZE(irq_remap_fault_reasons))) {
  1247. *fault_type = INTR_REMAP;
  1248. return irq_remap_fault_reasons[fault_reason - 0x20];
  1249. } else if (fault_reason < ARRAY_SIZE(dma_remap_fault_reasons)) {
  1250. *fault_type = DMA_REMAP;
  1251. return dma_remap_fault_reasons[fault_reason];
  1252. } else {
  1253. *fault_type = UNKNOWN;
  1254. return "Unknown";
  1255. }
  1256. }
  1257. static inline int dmar_msi_reg(struct intel_iommu *iommu, int irq)
  1258. {
  1259. if (iommu->irq == irq)
  1260. return DMAR_FECTL_REG;
  1261. else if (iommu->pr_irq == irq)
  1262. return DMAR_PECTL_REG;
  1263. else
  1264. BUG();
  1265. }
  1266. void dmar_msi_unmask(struct irq_data *data)
  1267. {
  1268. struct intel_iommu *iommu = irq_data_get_irq_handler_data(data);
  1269. int reg = dmar_msi_reg(iommu, data->irq);
  1270. unsigned long flag;
  1271. /* unmask it */
  1272. raw_spin_lock_irqsave(&iommu->register_lock, flag);
  1273. writel(0, iommu->reg + reg);
  1274. /* Read a reg to force flush the post write */
  1275. readl(iommu->reg + reg);
  1276. raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
  1277. }
  1278. void dmar_msi_mask(struct irq_data *data)
  1279. {
  1280. struct intel_iommu *iommu = irq_data_get_irq_handler_data(data);
  1281. int reg = dmar_msi_reg(iommu, data->irq);
  1282. unsigned long flag;
  1283. /* mask it */
  1284. raw_spin_lock_irqsave(&iommu->register_lock, flag);
  1285. writel(DMA_FECTL_IM, iommu->reg + reg);
  1286. /* Read a reg to force flush the post write */
  1287. readl(iommu->reg + reg);
  1288. raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
  1289. }
  1290. void dmar_msi_write(int irq, struct msi_msg *msg)
  1291. {
  1292. struct intel_iommu *iommu = irq_get_handler_data(irq);
  1293. int reg = dmar_msi_reg(iommu, irq);
  1294. unsigned long flag;
  1295. raw_spin_lock_irqsave(&iommu->register_lock, flag);
  1296. writel(msg->data, iommu->reg + reg + 4);
  1297. writel(msg->address_lo, iommu->reg + reg + 8);
  1298. writel(msg->address_hi, iommu->reg + reg + 12);
  1299. raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
  1300. }
  1301. void dmar_msi_read(int irq, struct msi_msg *msg)
  1302. {
  1303. struct intel_iommu *iommu = irq_get_handler_data(irq);
  1304. int reg = dmar_msi_reg(iommu, irq);
  1305. unsigned long flag;
  1306. raw_spin_lock_irqsave(&iommu->register_lock, flag);
  1307. msg->data = readl(iommu->reg + reg + 4);
  1308. msg->address_lo = readl(iommu->reg + reg + 8);
  1309. msg->address_hi = readl(iommu->reg + reg + 12);
  1310. raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
  1311. }
  1312. static int dmar_fault_do_one(struct intel_iommu *iommu, int type,
  1313. u8 fault_reason, u16 source_id, unsigned long long addr)
  1314. {
  1315. const char *reason;
  1316. int fault_type;
  1317. reason = dmar_get_fault_reason(fault_reason, &fault_type);
  1318. if (fault_type == INTR_REMAP)
  1319. pr_err("INTR-REMAP: Request device [[%02x:%02x.%d] "
  1320. "fault index %llx\n"
  1321. "INTR-REMAP:[fault reason %02d] %s\n",
  1322. (source_id >> 8), PCI_SLOT(source_id & 0xFF),
  1323. PCI_FUNC(source_id & 0xFF), addr >> 48,
  1324. fault_reason, reason);
  1325. else
  1326. pr_err("DMAR:[%s] Request device [%02x:%02x.%d] "
  1327. "fault addr %llx \n"
  1328. "DMAR:[fault reason %02d] %s\n",
  1329. (type ? "DMA Read" : "DMA Write"),
  1330. (source_id >> 8), PCI_SLOT(source_id & 0xFF),
  1331. PCI_FUNC(source_id & 0xFF), addr, fault_reason, reason);
  1332. return 0;
  1333. }
  1334. #define PRIMARY_FAULT_REG_LEN (16)
  1335. irqreturn_t dmar_fault(int irq, void *dev_id)
  1336. {
  1337. struct intel_iommu *iommu = dev_id;
  1338. int reg, fault_index;
  1339. u32 fault_status;
  1340. unsigned long flag;
  1341. raw_spin_lock_irqsave(&iommu->register_lock, flag);
  1342. fault_status = readl(iommu->reg + DMAR_FSTS_REG);
  1343. if (fault_status)
  1344. pr_err("DRHD: handling fault status reg %x\n", fault_status);
  1345. /* TBD: ignore advanced fault log currently */
  1346. if (!(fault_status & DMA_FSTS_PPF))
  1347. goto unlock_exit;
  1348. fault_index = dma_fsts_fault_record_index(fault_status);
  1349. reg = cap_fault_reg_offset(iommu->cap);
  1350. while (1) {
  1351. u8 fault_reason;
  1352. u16 source_id;
  1353. u64 guest_addr;
  1354. int type;
  1355. u32 data;
  1356. /* highest 32 bits */
  1357. data = readl(iommu->reg + reg +
  1358. fault_index * PRIMARY_FAULT_REG_LEN + 12);
  1359. if (!(data & DMA_FRCD_F))
  1360. break;
  1361. fault_reason = dma_frcd_fault_reason(data);
  1362. type = dma_frcd_type(data);
  1363. data = readl(iommu->reg + reg +
  1364. fault_index * PRIMARY_FAULT_REG_LEN + 8);
  1365. source_id = dma_frcd_source_id(data);
  1366. guest_addr = dmar_readq(iommu->reg + reg +
  1367. fault_index * PRIMARY_FAULT_REG_LEN);
  1368. guest_addr = dma_frcd_page_addr(guest_addr);
  1369. /* clear the fault */
  1370. writel(DMA_FRCD_F, iommu->reg + reg +
  1371. fault_index * PRIMARY_FAULT_REG_LEN + 12);
  1372. raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
  1373. dmar_fault_do_one(iommu, type, fault_reason,
  1374. source_id, guest_addr);
  1375. fault_index++;
  1376. if (fault_index >= cap_num_fault_regs(iommu->cap))
  1377. fault_index = 0;
  1378. raw_spin_lock_irqsave(&iommu->register_lock, flag);
  1379. }
  1380. writel(DMA_FSTS_PFO | DMA_FSTS_PPF, iommu->reg + DMAR_FSTS_REG);
  1381. unlock_exit:
  1382. raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
  1383. return IRQ_HANDLED;
  1384. }
  1385. int dmar_set_interrupt(struct intel_iommu *iommu)
  1386. {
  1387. int irq, ret;
  1388. /*
  1389. * Check if the fault interrupt is already initialized.
  1390. */
  1391. if (iommu->irq)
  1392. return 0;
  1393. irq = dmar_alloc_hwirq(iommu->seq_id, iommu->node, iommu);
  1394. if (irq > 0) {
  1395. iommu->irq = irq;
  1396. } else {
  1397. pr_err("No free IRQ vectors\n");
  1398. return -EINVAL;
  1399. }
  1400. ret = request_irq(irq, dmar_fault, IRQF_NO_THREAD, iommu->name, iommu);
  1401. if (ret)
  1402. pr_err("Can't request irq\n");
  1403. return ret;
  1404. }
  1405. int __init enable_drhd_fault_handling(void)
  1406. {
  1407. struct dmar_drhd_unit *drhd;
  1408. struct intel_iommu *iommu;
  1409. /*
  1410. * Enable fault control interrupt.
  1411. */
  1412. for_each_iommu(iommu, drhd) {
  1413. u32 fault_status;
  1414. int ret = dmar_set_interrupt(iommu);
  1415. if (ret) {
  1416. pr_err("DRHD %Lx: failed to enable fault, interrupt, ret %d\n",
  1417. (unsigned long long)drhd->reg_base_addr, ret);
  1418. return -1;
  1419. }
  1420. /*
  1421. * Clear any previous faults.
  1422. */
  1423. dmar_fault(iommu->irq, iommu);
  1424. fault_status = readl(iommu->reg + DMAR_FSTS_REG);
  1425. writel(fault_status, iommu->reg + DMAR_FSTS_REG);
  1426. }
  1427. return 0;
  1428. }
  1429. /*
  1430. * Re-enable Queued Invalidation interface.
  1431. */
  1432. int dmar_reenable_qi(struct intel_iommu *iommu)
  1433. {
  1434. if (!ecap_qis(iommu->ecap))
  1435. return -ENOENT;
  1436. if (!iommu->qi)
  1437. return -ENOENT;
  1438. /*
  1439. * First disable queued invalidation.
  1440. */
  1441. dmar_disable_qi(iommu);
  1442. /*
  1443. * Then enable queued invalidation again. Since there is no pending
  1444. * invalidation requests now, it's safe to re-enable queued
  1445. * invalidation.
  1446. */
  1447. __dmar_enable_qi(iommu);
  1448. return 0;
  1449. }
  1450. /*
  1451. * Check interrupt remapping support in DMAR table description.
  1452. */
  1453. int __init dmar_ir_support(void)
  1454. {
  1455. struct acpi_table_dmar *dmar;
  1456. dmar = (struct acpi_table_dmar *)dmar_tbl;
  1457. if (!dmar)
  1458. return 0;
  1459. return dmar->flags & 0x1;
  1460. }
  1461. /* Check whether DMAR units are in use */
  1462. static inline bool dmar_in_use(void)
  1463. {
  1464. return irq_remapping_enabled || intel_iommu_enabled;
  1465. }
  1466. static int __init dmar_free_unused_resources(void)
  1467. {
  1468. struct dmar_drhd_unit *dmaru, *dmaru_n;
  1469. if (dmar_in_use())
  1470. return 0;
  1471. if (dmar_dev_scope_status != 1 && !list_empty(&dmar_drhd_units))
  1472. bus_unregister_notifier(&pci_bus_type, &dmar_pci_bus_nb);
  1473. down_write(&dmar_global_lock);
  1474. list_for_each_entry_safe(dmaru, dmaru_n, &dmar_drhd_units, list) {
  1475. list_del(&dmaru->list);
  1476. dmar_free_drhd(dmaru);
  1477. }
  1478. up_write(&dmar_global_lock);
  1479. return 0;
  1480. }
  1481. late_initcall(dmar_free_unused_resources);
  1482. IOMMU_INIT_POST(detect_intel_iommu);
  1483. /*
  1484. * DMAR Hotplug Support
  1485. * For more details, please refer to Intel(R) Virtualization Technology
  1486. * for Directed-IO Architecture Specifiction, Rev 2.2, Section 8.8
  1487. * "Remapping Hardware Unit Hot Plug".
  1488. */
  1489. static u8 dmar_hp_uuid[] = {
  1490. /* 0000 */ 0xA6, 0xA3, 0xC1, 0xD8, 0x9B, 0xBE, 0x9B, 0x4C,
  1491. /* 0008 */ 0x91, 0xBF, 0xC3, 0xCB, 0x81, 0xFC, 0x5D, 0xAF
  1492. };
  1493. /*
  1494. * Currently there's only one revision and BIOS will not check the revision id,
  1495. * so use 0 for safety.
  1496. */
  1497. #define DMAR_DSM_REV_ID 0
  1498. #define DMAR_DSM_FUNC_DRHD 1
  1499. #define DMAR_DSM_FUNC_ATSR 2
  1500. #define DMAR_DSM_FUNC_RHSA 3
  1501. static inline bool dmar_detect_dsm(acpi_handle handle, int func)
  1502. {
  1503. return acpi_check_dsm(handle, dmar_hp_uuid, DMAR_DSM_REV_ID, 1 << func);
  1504. }
  1505. static int dmar_walk_dsm_resource(acpi_handle handle, int func,
  1506. dmar_res_handler_t handler, void *arg)
  1507. {
  1508. int ret = -ENODEV;
  1509. union acpi_object *obj;
  1510. struct acpi_dmar_header *start;
  1511. struct dmar_res_callback callback;
  1512. static int res_type[] = {
  1513. [DMAR_DSM_FUNC_DRHD] = ACPI_DMAR_TYPE_HARDWARE_UNIT,
  1514. [DMAR_DSM_FUNC_ATSR] = ACPI_DMAR_TYPE_ROOT_ATS,
  1515. [DMAR_DSM_FUNC_RHSA] = ACPI_DMAR_TYPE_HARDWARE_AFFINITY,
  1516. };
  1517. if (!dmar_detect_dsm(handle, func))
  1518. return 0;
  1519. obj = acpi_evaluate_dsm_typed(handle, dmar_hp_uuid, DMAR_DSM_REV_ID,
  1520. func, NULL, ACPI_TYPE_BUFFER);
  1521. if (!obj)
  1522. return -ENODEV;
  1523. memset(&callback, 0, sizeof(callback));
  1524. callback.cb[res_type[func]] = handler;
  1525. callback.arg[res_type[func]] = arg;
  1526. start = (struct acpi_dmar_header *)obj->buffer.pointer;
  1527. ret = dmar_walk_remapping_entries(start, obj->buffer.length, &callback);
  1528. ACPI_FREE(obj);
  1529. return ret;
  1530. }
  1531. static int dmar_hp_add_drhd(struct acpi_dmar_header *header, void *arg)
  1532. {
  1533. int ret;
  1534. struct dmar_drhd_unit *dmaru;
  1535. dmaru = dmar_find_dmaru((struct acpi_dmar_hardware_unit *)header);
  1536. if (!dmaru)
  1537. return -ENODEV;
  1538. ret = dmar_ir_hotplug(dmaru, true);
  1539. if (ret == 0)
  1540. ret = dmar_iommu_hotplug(dmaru, true);
  1541. return ret;
  1542. }
  1543. static int dmar_hp_remove_drhd(struct acpi_dmar_header *header, void *arg)
  1544. {
  1545. int i, ret;
  1546. struct device *dev;
  1547. struct dmar_drhd_unit *dmaru;
  1548. dmaru = dmar_find_dmaru((struct acpi_dmar_hardware_unit *)header);
  1549. if (!dmaru)
  1550. return 0;
  1551. /*
  1552. * All PCI devices managed by this unit should have been destroyed.
  1553. */
  1554. if (!dmaru->include_all && dmaru->devices && dmaru->devices_cnt) {
  1555. for_each_active_dev_scope(dmaru->devices,
  1556. dmaru->devices_cnt, i, dev)
  1557. return -EBUSY;
  1558. }
  1559. ret = dmar_ir_hotplug(dmaru, false);
  1560. if (ret == 0)
  1561. ret = dmar_iommu_hotplug(dmaru, false);
  1562. return ret;
  1563. }
  1564. static int dmar_hp_release_drhd(struct acpi_dmar_header *header, void *arg)
  1565. {
  1566. struct dmar_drhd_unit *dmaru;
  1567. dmaru = dmar_find_dmaru((struct acpi_dmar_hardware_unit *)header);
  1568. if (dmaru) {
  1569. list_del_rcu(&dmaru->list);
  1570. synchronize_rcu();
  1571. dmar_free_drhd(dmaru);
  1572. }
  1573. return 0;
  1574. }
  1575. static int dmar_hotplug_insert(acpi_handle handle)
  1576. {
  1577. int ret;
  1578. int drhd_count = 0;
  1579. ret = dmar_walk_dsm_resource(handle, DMAR_DSM_FUNC_DRHD,
  1580. &dmar_validate_one_drhd, (void *)1);
  1581. if (ret)
  1582. goto out;
  1583. ret = dmar_walk_dsm_resource(handle, DMAR_DSM_FUNC_DRHD,
  1584. &dmar_parse_one_drhd, (void *)&drhd_count);
  1585. if (ret == 0 && drhd_count == 0) {
  1586. pr_warn(FW_BUG "No DRHD structures in buffer returned by _DSM method\n");
  1587. goto out;
  1588. } else if (ret) {
  1589. goto release_drhd;
  1590. }
  1591. ret = dmar_walk_dsm_resource(handle, DMAR_DSM_FUNC_RHSA,
  1592. &dmar_parse_one_rhsa, NULL);
  1593. if (ret)
  1594. goto release_drhd;
  1595. ret = dmar_walk_dsm_resource(handle, DMAR_DSM_FUNC_ATSR,
  1596. &dmar_parse_one_atsr, NULL);
  1597. if (ret)
  1598. goto release_atsr;
  1599. ret = dmar_walk_dsm_resource(handle, DMAR_DSM_FUNC_DRHD,
  1600. &dmar_hp_add_drhd, NULL);
  1601. if (!ret)
  1602. return 0;
  1603. dmar_walk_dsm_resource(handle, DMAR_DSM_FUNC_DRHD,
  1604. &dmar_hp_remove_drhd, NULL);
  1605. release_atsr:
  1606. dmar_walk_dsm_resource(handle, DMAR_DSM_FUNC_ATSR,
  1607. &dmar_release_one_atsr, NULL);
  1608. release_drhd:
  1609. dmar_walk_dsm_resource(handle, DMAR_DSM_FUNC_DRHD,
  1610. &dmar_hp_release_drhd, NULL);
  1611. out:
  1612. return ret;
  1613. }
  1614. static int dmar_hotplug_remove(acpi_handle handle)
  1615. {
  1616. int ret;
  1617. ret = dmar_walk_dsm_resource(handle, DMAR_DSM_FUNC_ATSR,
  1618. &dmar_check_one_atsr, NULL);
  1619. if (ret)
  1620. return ret;
  1621. ret = dmar_walk_dsm_resource(handle, DMAR_DSM_FUNC_DRHD,
  1622. &dmar_hp_remove_drhd, NULL);
  1623. if (ret == 0) {
  1624. WARN_ON(dmar_walk_dsm_resource(handle, DMAR_DSM_FUNC_ATSR,
  1625. &dmar_release_one_atsr, NULL));
  1626. WARN_ON(dmar_walk_dsm_resource(handle, DMAR_DSM_FUNC_DRHD,
  1627. &dmar_hp_release_drhd, NULL));
  1628. } else {
  1629. dmar_walk_dsm_resource(handle, DMAR_DSM_FUNC_DRHD,
  1630. &dmar_hp_add_drhd, NULL);
  1631. }
  1632. return ret;
  1633. }
  1634. static acpi_status dmar_get_dsm_handle(acpi_handle handle, u32 lvl,
  1635. void *context, void **retval)
  1636. {
  1637. acpi_handle *phdl = retval;
  1638. if (dmar_detect_dsm(handle, DMAR_DSM_FUNC_DRHD)) {
  1639. *phdl = handle;
  1640. return AE_CTRL_TERMINATE;
  1641. }
  1642. return AE_OK;
  1643. }
  1644. static int dmar_device_hotplug(acpi_handle handle, bool insert)
  1645. {
  1646. int ret;
  1647. acpi_handle tmp = NULL;
  1648. acpi_status status;
  1649. if (!dmar_in_use())
  1650. return 0;
  1651. if (dmar_detect_dsm(handle, DMAR_DSM_FUNC_DRHD)) {
  1652. tmp = handle;
  1653. } else {
  1654. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  1655. ACPI_UINT32_MAX,
  1656. dmar_get_dsm_handle,
  1657. NULL, NULL, &tmp);
  1658. if (ACPI_FAILURE(status)) {
  1659. pr_warn("Failed to locate _DSM method.\n");
  1660. return -ENXIO;
  1661. }
  1662. }
  1663. if (tmp == NULL)
  1664. return 0;
  1665. down_write(&dmar_global_lock);
  1666. if (insert)
  1667. ret = dmar_hotplug_insert(tmp);
  1668. else
  1669. ret = dmar_hotplug_remove(tmp);
  1670. up_write(&dmar_global_lock);
  1671. return ret;
  1672. }
  1673. int dmar_device_add(acpi_handle handle)
  1674. {
  1675. return dmar_device_hotplug(handle, true);
  1676. }
  1677. int dmar_device_remove(acpi_handle handle)
  1678. {
  1679. return dmar_device_hotplug(handle, false);
  1680. }