dmar.c 49 KB

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