acpiphp_glue.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. /*
  2. * ACPI PCI HotPlug glue functions to ACPI CA subsystem
  3. *
  4. * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
  5. * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
  6. * Copyright (C) 2002,2003 NEC Corporation
  7. * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
  8. * Copyright (C) 2003-2005 Hewlett Packard
  9. * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
  10. * Copyright (C) 2005 Intel Corporation
  11. *
  12. * All rights reserved.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or (at
  17. * your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  22. * NON INFRINGEMENT. See the GNU General Public License for more
  23. * details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. *
  29. * Send feedback to <kristen.c.accardi@intel.com>
  30. *
  31. */
  32. /*
  33. * Lifetime rules for pci_dev:
  34. * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
  35. * when the bridge is scanned and it loses a refcount when the bridge
  36. * is removed.
  37. * - When a P2P bridge is present, we elevate the refcount on the subordinate
  38. * bus. It loses the refcount when the the driver unloads.
  39. */
  40. #define pr_fmt(fmt) "acpiphp_glue: " fmt
  41. #include <linux/init.h>
  42. #include <linux/module.h>
  43. #include <linux/kernel.h>
  44. #include <linux/pci.h>
  45. #include <linux/pci_hotplug.h>
  46. #include <linux/pci-acpi.h>
  47. #include <linux/pm_runtime.h>
  48. #include <linux/mutex.h>
  49. #include <linux/slab.h>
  50. #include <linux/acpi.h>
  51. #include "../pci.h"
  52. #include "acpiphp.h"
  53. static LIST_HEAD(bridge_list);
  54. static DEFINE_MUTEX(bridge_mutex);
  55. static DEFINE_MUTEX(acpiphp_context_lock);
  56. static void handle_hotplug_event(acpi_handle handle, u32 type, void *data);
  57. static void acpiphp_sanitize_bus(struct pci_bus *bus);
  58. static void acpiphp_set_hpp_values(struct pci_bus *bus);
  59. static void hotplug_event(acpi_handle handle, u32 type, void *data);
  60. static void free_bridge(struct kref *kref);
  61. static void acpiphp_context_handler(acpi_handle handle, void *context)
  62. {
  63. /* Intentionally empty. */
  64. }
  65. /**
  66. * acpiphp_init_context - Create hotplug context and grab a reference to it.
  67. * @handle: ACPI object handle to create the context for.
  68. *
  69. * Call under acpiphp_context_lock.
  70. */
  71. static struct acpiphp_context *acpiphp_init_context(acpi_handle handle)
  72. {
  73. struct acpiphp_context *context;
  74. acpi_status status;
  75. context = kzalloc(sizeof(*context), GFP_KERNEL);
  76. if (!context)
  77. return NULL;
  78. context->handle = handle;
  79. context->refcount = 1;
  80. status = acpi_attach_data(handle, acpiphp_context_handler, context);
  81. if (ACPI_FAILURE(status)) {
  82. kfree(context);
  83. return NULL;
  84. }
  85. return context;
  86. }
  87. /**
  88. * acpiphp_get_context - Get hotplug context and grab a reference to it.
  89. * @handle: ACPI object handle to get the context for.
  90. *
  91. * Call under acpiphp_context_lock.
  92. */
  93. static struct acpiphp_context *acpiphp_get_context(acpi_handle handle)
  94. {
  95. struct acpiphp_context *context = NULL;
  96. acpi_status status;
  97. void *data;
  98. status = acpi_get_data(handle, acpiphp_context_handler, &data);
  99. if (ACPI_SUCCESS(status)) {
  100. context = data;
  101. context->refcount++;
  102. }
  103. return context;
  104. }
  105. /**
  106. * acpiphp_put_context - Drop a reference to ACPI hotplug context.
  107. * @handle: ACPI object handle to put the context for.
  108. *
  109. * The context object is removed if there are no more references to it.
  110. *
  111. * Call under acpiphp_context_lock.
  112. */
  113. static void acpiphp_put_context(struct acpiphp_context *context)
  114. {
  115. if (--context->refcount)
  116. return;
  117. WARN_ON(context->bridge);
  118. acpi_detach_data(context->handle, acpiphp_context_handler);
  119. kfree(context);
  120. }
  121. static inline void get_bridge(struct acpiphp_bridge *bridge)
  122. {
  123. kref_get(&bridge->ref);
  124. }
  125. static inline void put_bridge(struct acpiphp_bridge *bridge)
  126. {
  127. kref_put(&bridge->ref, free_bridge);
  128. }
  129. static void free_bridge(struct kref *kref)
  130. {
  131. struct acpiphp_context *context;
  132. struct acpiphp_bridge *bridge;
  133. struct acpiphp_slot *slot, *next;
  134. struct acpiphp_func *func, *tmp;
  135. mutex_lock(&acpiphp_context_lock);
  136. bridge = container_of(kref, struct acpiphp_bridge, ref);
  137. list_for_each_entry_safe(slot, next, &bridge->slots, node) {
  138. list_for_each_entry_safe(func, tmp, &slot->funcs, sibling)
  139. acpiphp_put_context(func_to_context(func));
  140. kfree(slot);
  141. }
  142. context = bridge->context;
  143. /* Root bridges will not have hotplug context. */
  144. if (context) {
  145. /* Release the reference taken by acpiphp_enumerate_slots(). */
  146. put_bridge(context->func.parent);
  147. context->bridge = NULL;
  148. acpiphp_put_context(context);
  149. }
  150. put_device(&bridge->pci_bus->dev);
  151. pci_dev_put(bridge->pci_dev);
  152. kfree(bridge);
  153. mutex_unlock(&acpiphp_context_lock);
  154. }
  155. /*
  156. * the _DCK method can do funny things... and sometimes not
  157. * hah-hah funny.
  158. *
  159. * TBD - figure out a way to only call fixups for
  160. * systems that require them.
  161. */
  162. static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
  163. {
  164. struct acpiphp_context *context = data;
  165. struct pci_bus *bus = context->func.slot->bus;
  166. u32 buses;
  167. if (!bus->self)
  168. return;
  169. /* fixup bad _DCK function that rewrites
  170. * secondary bridge on slot
  171. */
  172. pci_read_config_dword(bus->self,
  173. PCI_PRIMARY_BUS,
  174. &buses);
  175. if (((buses >> 8) & 0xff) != bus->busn_res.start) {
  176. buses = (buses & 0xff000000)
  177. | ((unsigned int)(bus->primary) << 0)
  178. | ((unsigned int)(bus->busn_res.start) << 8)
  179. | ((unsigned int)(bus->busn_res.end) << 16);
  180. pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
  181. }
  182. }
  183. static void dock_event(acpi_handle handle, u32 type, void *data)
  184. {
  185. struct acpiphp_context *context;
  186. mutex_lock(&acpiphp_context_lock);
  187. context = acpiphp_get_context(handle);
  188. if (!context || WARN_ON(context->handle != handle)
  189. || context->func.parent->is_going_away) {
  190. mutex_unlock(&acpiphp_context_lock);
  191. return;
  192. }
  193. get_bridge(context->func.parent);
  194. acpiphp_put_context(context);
  195. mutex_unlock(&acpiphp_context_lock);
  196. hotplug_event(handle, type, data);
  197. put_bridge(context->func.parent);
  198. }
  199. static const struct acpi_dock_ops acpiphp_dock_ops = {
  200. .fixup = post_dock_fixups,
  201. .handler = dock_event,
  202. };
  203. /* Check whether the PCI device is managed by native PCIe hotplug driver */
  204. static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
  205. {
  206. u32 reg32;
  207. acpi_handle tmp;
  208. struct acpi_pci_root *root;
  209. /* Check whether the PCIe port supports native PCIe hotplug */
  210. if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
  211. return false;
  212. if (!(reg32 & PCI_EXP_SLTCAP_HPC))
  213. return false;
  214. /*
  215. * Check whether native PCIe hotplug has been enabled for
  216. * this PCIe hierarchy.
  217. */
  218. tmp = acpi_find_root_bridge_handle(pdev);
  219. if (!tmp)
  220. return false;
  221. root = acpi_pci_find_root(tmp);
  222. if (!root)
  223. return false;
  224. if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
  225. return false;
  226. return true;
  227. }
  228. static void acpiphp_dock_init(void *data)
  229. {
  230. struct acpiphp_context *context = data;
  231. get_bridge(context->func.parent);
  232. }
  233. static void acpiphp_dock_release(void *data)
  234. {
  235. struct acpiphp_context *context = data;
  236. put_bridge(context->func.parent);
  237. }
  238. /* callback routine to register each ACPI PCI slot object */
  239. static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data,
  240. void **rv)
  241. {
  242. struct acpiphp_bridge *bridge = data;
  243. struct acpiphp_context *context;
  244. struct acpiphp_slot *slot;
  245. struct acpiphp_func *newfunc;
  246. acpi_status status = AE_OK;
  247. unsigned long long adr;
  248. int device, function;
  249. struct pci_bus *pbus = bridge->pci_bus;
  250. struct pci_dev *pdev = bridge->pci_dev;
  251. u32 val;
  252. if (pdev && device_is_managed_by_native_pciehp(pdev))
  253. return AE_OK;
  254. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  255. if (ACPI_FAILURE(status)) {
  256. if (status != AE_NOT_FOUND)
  257. acpi_handle_warn(handle,
  258. "can't evaluate _ADR (%#x)\n", status);
  259. return AE_OK;
  260. }
  261. device = (adr >> 16) & 0xffff;
  262. function = adr & 0xffff;
  263. mutex_lock(&acpiphp_context_lock);
  264. context = acpiphp_init_context(handle);
  265. if (!context) {
  266. mutex_unlock(&acpiphp_context_lock);
  267. acpi_handle_err(handle, "No hotplug context\n");
  268. return AE_NOT_EXIST;
  269. }
  270. newfunc = &context->func;
  271. newfunc->function = function;
  272. newfunc->parent = bridge;
  273. mutex_unlock(&acpiphp_context_lock);
  274. if (acpi_has_method(handle, "_EJ0"))
  275. newfunc->flags = FUNC_HAS_EJ0;
  276. if (acpi_has_method(handle, "_STA"))
  277. newfunc->flags |= FUNC_HAS_STA;
  278. if (acpi_has_method(handle, "_DCK"))
  279. newfunc->flags |= FUNC_HAS_DCK;
  280. /* search for objects that share the same slot */
  281. list_for_each_entry(slot, &bridge->slots, node)
  282. if (slot->device == device)
  283. goto slot_found;
  284. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  285. if (!slot) {
  286. status = AE_NO_MEMORY;
  287. goto err;
  288. }
  289. slot->bus = bridge->pci_bus;
  290. slot->device = device;
  291. INIT_LIST_HEAD(&slot->funcs);
  292. mutex_init(&slot->crit_sect);
  293. list_add_tail(&slot->node, &bridge->slots);
  294. /* Register slots for ejectable functions only. */
  295. if (acpi_pci_check_ejectable(pbus, handle) || is_dock_device(handle)) {
  296. unsigned long long sun;
  297. int retval;
  298. bridge->nr_slots++;
  299. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  300. if (ACPI_FAILURE(status))
  301. sun = bridge->nr_slots;
  302. pr_debug("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
  303. sun, pci_domain_nr(pbus), pbus->number, device);
  304. retval = acpiphp_register_hotplug_slot(slot, sun);
  305. if (retval) {
  306. slot->slot = NULL;
  307. bridge->nr_slots--;
  308. if (retval == -EBUSY)
  309. pr_warn("Slot %llu already registered by another "
  310. "hotplug driver\n", sun);
  311. else
  312. pr_warn("acpiphp_register_hotplug_slot failed "
  313. "(err code = 0x%x)\n", retval);
  314. }
  315. /* Even if the slot registration fails, we can still use it. */
  316. }
  317. slot_found:
  318. newfunc->slot = slot;
  319. list_add_tail(&newfunc->sibling, &slot->funcs);
  320. if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
  321. &val, 60*1000))
  322. slot->flags |= SLOT_ENABLED;
  323. if (is_dock_device(handle)) {
  324. /* we don't want to call this device's _EJ0
  325. * because we want the dock notify handler
  326. * to call it after it calls _DCK
  327. */
  328. newfunc->flags &= ~FUNC_HAS_EJ0;
  329. if (register_hotplug_dock_device(handle,
  330. &acpiphp_dock_ops, context,
  331. acpiphp_dock_init, acpiphp_dock_release))
  332. pr_debug("failed to register dock device\n");
  333. }
  334. /* install notify handler */
  335. if (!(newfunc->flags & FUNC_HAS_DCK)) {
  336. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  337. handle_hotplug_event,
  338. context);
  339. if (ACPI_FAILURE(status))
  340. acpi_handle_err(handle,
  341. "failed to install notify handler\n");
  342. }
  343. return AE_OK;
  344. err:
  345. mutex_lock(&acpiphp_context_lock);
  346. acpiphp_put_context(context);
  347. mutex_unlock(&acpiphp_context_lock);
  348. return status;
  349. }
  350. static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
  351. {
  352. struct acpiphp_context *context;
  353. struct acpiphp_bridge *bridge = NULL;
  354. mutex_lock(&acpiphp_context_lock);
  355. context = acpiphp_get_context(handle);
  356. if (context) {
  357. bridge = context->bridge;
  358. if (bridge)
  359. get_bridge(bridge);
  360. acpiphp_put_context(context);
  361. }
  362. mutex_unlock(&acpiphp_context_lock);
  363. return bridge;
  364. }
  365. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  366. {
  367. struct acpiphp_slot *slot;
  368. struct acpiphp_func *func;
  369. acpi_status status;
  370. list_for_each_entry(slot, &bridge->slots, node) {
  371. list_for_each_entry(func, &slot->funcs, sibling) {
  372. acpi_handle handle = func_to_handle(func);
  373. if (is_dock_device(handle))
  374. unregister_hotplug_dock_device(handle);
  375. if (!(func->flags & FUNC_HAS_DCK)) {
  376. status = acpi_remove_notify_handler(handle,
  377. ACPI_SYSTEM_NOTIFY,
  378. handle_hotplug_event);
  379. if (ACPI_FAILURE(status))
  380. pr_err("failed to remove notify handler\n");
  381. }
  382. }
  383. slot->flags |= SLOT_IS_GOING_AWAY;
  384. if (slot->slot)
  385. acpiphp_unregister_hotplug_slot(slot);
  386. }
  387. mutex_lock(&bridge_mutex);
  388. list_del(&bridge->list);
  389. mutex_unlock(&bridge_mutex);
  390. mutex_lock(&acpiphp_context_lock);
  391. bridge->is_going_away = true;
  392. mutex_unlock(&acpiphp_context_lock);
  393. }
  394. /**
  395. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  396. * @bus: bus to start search with
  397. */
  398. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  399. {
  400. struct list_head *tmp;
  401. unsigned char max, n;
  402. /*
  403. * pci_bus_max_busnr will return the highest
  404. * reserved busnr for all these children.
  405. * that is equivalent to the bus->subordinate
  406. * value. We don't want to use the parent's
  407. * bus->subordinate value because it could have
  408. * padding in it.
  409. */
  410. max = bus->busn_res.start;
  411. list_for_each(tmp, &bus->children) {
  412. n = pci_bus_max_busnr(pci_bus_b(tmp));
  413. if (n > max)
  414. max = n;
  415. }
  416. return max;
  417. }
  418. /**
  419. * acpiphp_bus_trim - Trim device objects in an ACPI namespace subtree.
  420. * @handle: ACPI device object handle to start from.
  421. */
  422. static void acpiphp_bus_trim(acpi_handle handle)
  423. {
  424. struct acpi_device *adev = NULL;
  425. acpi_bus_get_device(handle, &adev);
  426. if (adev)
  427. acpi_bus_trim(adev);
  428. }
  429. /**
  430. * acpiphp_bus_add - Scan ACPI namespace subtree.
  431. * @handle: ACPI object handle to start the scan from.
  432. */
  433. static void acpiphp_bus_add(acpi_handle handle)
  434. {
  435. struct acpi_device *adev = NULL;
  436. acpi_bus_scan(handle);
  437. acpi_bus_get_device(handle, &adev);
  438. if (acpi_device_enumerated(adev))
  439. acpi_device_set_power(adev, ACPI_STATE_D0);
  440. }
  441. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  442. {
  443. struct acpiphp_func *func;
  444. union acpi_object params[2];
  445. struct acpi_object_list arg_list;
  446. list_for_each_entry(func, &slot->funcs, sibling) {
  447. arg_list.count = 2;
  448. arg_list.pointer = params;
  449. params[0].type = ACPI_TYPE_INTEGER;
  450. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  451. params[1].type = ACPI_TYPE_INTEGER;
  452. params[1].integer.value = 1;
  453. /* _REG is optional, we don't care about if there is failure */
  454. acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
  455. NULL);
  456. }
  457. }
  458. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  459. {
  460. struct acpiphp_func *func;
  461. /* quirk, or pcie could set it already */
  462. if (dev->is_hotplug_bridge)
  463. return;
  464. list_for_each_entry(func, &slot->funcs, sibling) {
  465. if (PCI_FUNC(dev->devfn) == func->function) {
  466. dev->is_hotplug_bridge = 1;
  467. break;
  468. }
  469. }
  470. }
  471. static int acpiphp_rescan_slot(struct acpiphp_slot *slot)
  472. {
  473. struct acpiphp_func *func;
  474. list_for_each_entry(func, &slot->funcs, sibling)
  475. acpiphp_bus_add(func_to_handle(func));
  476. return pci_scan_slot(slot->bus, PCI_DEVFN(slot->device, 0));
  477. }
  478. /**
  479. * enable_slot - enable, configure a slot
  480. * @slot: slot to be enabled
  481. *
  482. * This function should be called per *physical slot*,
  483. * not per each slot object in ACPI namespace.
  484. */
  485. static void __ref enable_slot(struct acpiphp_slot *slot)
  486. {
  487. struct pci_dev *dev;
  488. struct pci_bus *bus = slot->bus;
  489. struct acpiphp_func *func;
  490. int max, pass;
  491. LIST_HEAD(add_list);
  492. acpiphp_rescan_slot(slot);
  493. max = acpiphp_max_busnr(bus);
  494. for (pass = 0; pass < 2; pass++) {
  495. list_for_each_entry(dev, &bus->devices, bus_list) {
  496. if (PCI_SLOT(dev->devfn) != slot->device)
  497. continue;
  498. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  499. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  500. max = pci_scan_bridge(bus, dev, max, pass);
  501. if (pass && dev->subordinate) {
  502. check_hotplug_bridge(slot, dev);
  503. pcibios_resource_survey_bus(dev->subordinate);
  504. __pci_bus_size_bridges(dev->subordinate,
  505. &add_list);
  506. }
  507. }
  508. }
  509. }
  510. __pci_bus_assign_resources(bus, &add_list, NULL);
  511. acpiphp_sanitize_bus(bus);
  512. acpiphp_set_hpp_values(bus);
  513. acpiphp_set_acpi_region(slot);
  514. list_for_each_entry(dev, &bus->devices, bus_list) {
  515. /* Assume that newly added devices are powered on already. */
  516. if (!dev->is_added)
  517. dev->current_state = PCI_D0;
  518. }
  519. pci_bus_add_devices(bus);
  520. slot->flags |= SLOT_ENABLED;
  521. list_for_each_entry(func, &slot->funcs, sibling) {
  522. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  523. func->function));
  524. if (!dev) {
  525. /* Do not set SLOT_ENABLED flag if some funcs
  526. are not added. */
  527. slot->flags &= (~SLOT_ENABLED);
  528. continue;
  529. }
  530. }
  531. }
  532. /* return first device in slot, acquiring a reference on it */
  533. static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
  534. {
  535. struct pci_bus *bus = slot->bus;
  536. struct pci_dev *dev;
  537. struct pci_dev *ret = NULL;
  538. down_read(&pci_bus_sem);
  539. list_for_each_entry(dev, &bus->devices, bus_list)
  540. if (PCI_SLOT(dev->devfn) == slot->device) {
  541. ret = pci_dev_get(dev);
  542. break;
  543. }
  544. up_read(&pci_bus_sem);
  545. return ret;
  546. }
  547. /**
  548. * disable_slot - disable a slot
  549. * @slot: ACPI PHP slot
  550. */
  551. static void disable_slot(struct acpiphp_slot *slot)
  552. {
  553. struct acpiphp_func *func;
  554. struct pci_dev *pdev;
  555. /*
  556. * enable_slot() enumerates all functions in this device via
  557. * pci_scan_slot(), whether they have associated ACPI hotplug
  558. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  559. * here.
  560. */
  561. while ((pdev = dev_in_slot(slot))) {
  562. pci_stop_and_remove_bus_device(pdev);
  563. pci_dev_put(pdev);
  564. }
  565. list_for_each_entry(func, &slot->funcs, sibling)
  566. acpiphp_bus_trim(func_to_handle(func));
  567. slot->flags &= (~SLOT_ENABLED);
  568. }
  569. static bool acpiphp_no_hotplug(acpi_handle handle)
  570. {
  571. struct acpi_device *adev = NULL;
  572. acpi_bus_get_device(handle, &adev);
  573. return adev && adev->flags.no_hotplug;
  574. }
  575. static bool slot_no_hotplug(struct acpiphp_slot *slot)
  576. {
  577. struct acpiphp_func *func;
  578. list_for_each_entry(func, &slot->funcs, sibling)
  579. if (acpiphp_no_hotplug(func_to_handle(func)))
  580. return true;
  581. return false;
  582. }
  583. /**
  584. * get_slot_status - get ACPI slot status
  585. * @slot: ACPI PHP slot
  586. *
  587. * If a slot has _STA for each function and if any one of them
  588. * returned non-zero status, return it.
  589. *
  590. * If a slot doesn't have _STA and if any one of its functions'
  591. * configuration space is configured, return 0x0f as a _STA.
  592. *
  593. * Otherwise return 0.
  594. */
  595. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  596. {
  597. unsigned long long sta = 0;
  598. struct acpiphp_func *func;
  599. list_for_each_entry(func, &slot->funcs, sibling) {
  600. if (func->flags & FUNC_HAS_STA) {
  601. acpi_status status;
  602. status = acpi_evaluate_integer(func_to_handle(func),
  603. "_STA", NULL, &sta);
  604. if (ACPI_SUCCESS(status) && sta)
  605. break;
  606. } else {
  607. u32 dvid;
  608. pci_bus_read_config_dword(slot->bus,
  609. PCI_DEVFN(slot->device,
  610. func->function),
  611. PCI_VENDOR_ID, &dvid);
  612. if (dvid != 0xffffffff) {
  613. sta = ACPI_STA_ALL;
  614. break;
  615. }
  616. }
  617. }
  618. return (unsigned int)sta;
  619. }
  620. static inline bool device_status_valid(unsigned int sta)
  621. {
  622. /*
  623. * ACPI spec says that _STA may return bit 0 clear with bit 3 set
  624. * if the device is valid but does not require a device driver to be
  625. * loaded (Section 6.3.7 of ACPI 5.0A).
  626. */
  627. unsigned int mask = ACPI_STA_DEVICE_ENABLED | ACPI_STA_DEVICE_FUNCTIONING;
  628. return (sta & mask) == mask;
  629. }
  630. /**
  631. * trim_stale_devices - remove PCI devices that are not responding.
  632. * @dev: PCI device to start walking the hierarchy from.
  633. */
  634. static void trim_stale_devices(struct pci_dev *dev)
  635. {
  636. acpi_handle handle = ACPI_HANDLE(&dev->dev);
  637. struct pci_bus *bus = dev->subordinate;
  638. bool alive = false;
  639. if (handle) {
  640. acpi_status status;
  641. unsigned long long sta;
  642. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  643. alive = (ACPI_SUCCESS(status) && device_status_valid(sta))
  644. || acpiphp_no_hotplug(handle);
  645. }
  646. if (!alive) {
  647. u32 v;
  648. /* Check if the device responds. */
  649. alive = pci_bus_read_dev_vendor_id(dev->bus, dev->devfn, &v, 0);
  650. }
  651. if (!alive) {
  652. pci_stop_and_remove_bus_device(dev);
  653. if (handle)
  654. acpiphp_bus_trim(handle);
  655. } else if (bus) {
  656. struct pci_dev *child, *tmp;
  657. /* The device is a bridge. so check the bus below it. */
  658. pm_runtime_get_sync(&dev->dev);
  659. list_for_each_entry_safe_reverse(child, tmp, &bus->devices, bus_list)
  660. trim_stale_devices(child);
  661. pm_runtime_put(&dev->dev);
  662. }
  663. }
  664. /**
  665. * acpiphp_check_bridge - re-enumerate devices
  666. * @bridge: where to begin re-enumeration
  667. *
  668. * Iterate over all slots under this bridge and make sure that if a
  669. * card is present they are enabled, and if not they are disabled.
  670. */
  671. static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  672. {
  673. struct acpiphp_slot *slot;
  674. /* Bail out if the bridge is going away. */
  675. if (bridge->is_going_away)
  676. return;
  677. list_for_each_entry(slot, &bridge->slots, node) {
  678. struct pci_bus *bus = slot->bus;
  679. struct pci_dev *dev, *tmp;
  680. mutex_lock(&slot->crit_sect);
  681. if (slot_no_hotplug(slot)) {
  682. ; /* do nothing */
  683. } else if (device_status_valid(get_slot_status(slot))) {
  684. /* remove stale devices if any */
  685. list_for_each_entry_safe_reverse(dev, tmp,
  686. &bus->devices, bus_list)
  687. if (PCI_SLOT(dev->devfn) == slot->device)
  688. trim_stale_devices(dev);
  689. /* configure all functions */
  690. enable_slot(slot);
  691. } else {
  692. disable_slot(slot);
  693. }
  694. mutex_unlock(&slot->crit_sect);
  695. }
  696. }
  697. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  698. {
  699. struct pci_dev *dev;
  700. list_for_each_entry(dev, &bus->devices, bus_list)
  701. pci_configure_slot(dev);
  702. }
  703. /*
  704. * Remove devices for which we could not assign resources, call
  705. * arch specific code to fix-up the bus
  706. */
  707. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  708. {
  709. struct pci_dev *dev, *tmp;
  710. int i;
  711. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  712. list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
  713. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  714. struct resource *res = &dev->resource[i];
  715. if ((res->flags & type_mask) && !res->start &&
  716. res->end) {
  717. /* Could not assign a required resources
  718. * for this device, remove it */
  719. pci_stop_and_remove_bus_device(dev);
  720. break;
  721. }
  722. }
  723. }
  724. }
  725. /*
  726. * ACPI event handlers
  727. */
  728. void acpiphp_check_host_bridge(acpi_handle handle)
  729. {
  730. struct acpiphp_bridge *bridge;
  731. bridge = acpiphp_handle_to_bridge(handle);
  732. if (bridge) {
  733. pci_lock_rescan_remove();
  734. acpiphp_check_bridge(bridge);
  735. pci_unlock_rescan_remove();
  736. put_bridge(bridge);
  737. }
  738. }
  739. static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot);
  740. static void hotplug_event(acpi_handle handle, u32 type, void *data)
  741. {
  742. struct acpiphp_context *context = data;
  743. struct acpiphp_func *func = &context->func;
  744. struct acpiphp_bridge *bridge;
  745. char objname[64];
  746. struct acpi_buffer buffer = { .length = sizeof(objname),
  747. .pointer = objname };
  748. mutex_lock(&acpiphp_context_lock);
  749. bridge = context->bridge;
  750. if (bridge)
  751. get_bridge(bridge);
  752. mutex_unlock(&acpiphp_context_lock);
  753. pci_lock_rescan_remove();
  754. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  755. switch (type) {
  756. case ACPI_NOTIFY_BUS_CHECK:
  757. /* bus re-enumerate */
  758. pr_debug("%s: Bus check notify on %s\n", __func__, objname);
  759. pr_debug("%s: re-enumerating slots under %s\n",
  760. __func__, objname);
  761. if (bridge) {
  762. acpiphp_check_bridge(bridge);
  763. } else {
  764. struct acpiphp_slot *slot = func->slot;
  765. if (slot->flags & SLOT_IS_GOING_AWAY)
  766. break;
  767. mutex_lock(&slot->crit_sect);
  768. enable_slot(slot);
  769. mutex_unlock(&slot->crit_sect);
  770. }
  771. break;
  772. case ACPI_NOTIFY_DEVICE_CHECK:
  773. /* device check */
  774. pr_debug("%s: Device check notify on %s\n", __func__, objname);
  775. if (bridge) {
  776. acpiphp_check_bridge(bridge);
  777. } else {
  778. struct acpiphp_slot *slot = func->slot;
  779. int ret;
  780. if (slot->flags & SLOT_IS_GOING_AWAY)
  781. break;
  782. /*
  783. * Check if anything has changed in the slot and rescan
  784. * from the parent if that's the case.
  785. */
  786. mutex_lock(&slot->crit_sect);
  787. ret = acpiphp_rescan_slot(slot);
  788. mutex_unlock(&slot->crit_sect);
  789. if (ret)
  790. acpiphp_check_bridge(func->parent);
  791. }
  792. break;
  793. case ACPI_NOTIFY_EJECT_REQUEST:
  794. /* request device eject */
  795. pr_debug("%s: Device eject notify on %s\n", __func__, objname);
  796. acpiphp_disable_and_eject_slot(func->slot);
  797. break;
  798. }
  799. pci_unlock_rescan_remove();
  800. if (bridge)
  801. put_bridge(bridge);
  802. }
  803. static void hotplug_event_work(void *data, u32 type)
  804. {
  805. struct acpiphp_context *context = data;
  806. acpi_handle handle = context->handle;
  807. acpi_scan_lock_acquire();
  808. hotplug_event(handle, type, context);
  809. acpi_scan_lock_release();
  810. acpi_evaluate_hotplug_ost(handle, type, ACPI_OST_SC_SUCCESS, NULL);
  811. put_bridge(context->func.parent);
  812. }
  813. /**
  814. * handle_hotplug_event - handle ACPI hotplug event
  815. * @handle: Notify()'ed acpi_handle
  816. * @type: Notify code
  817. * @data: pointer to acpiphp_context structure
  818. *
  819. * Handles ACPI event notification on slots.
  820. */
  821. static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
  822. {
  823. struct acpiphp_context *context;
  824. u32 ost_code = ACPI_OST_SC_SUCCESS;
  825. acpi_status status;
  826. switch (type) {
  827. case ACPI_NOTIFY_BUS_CHECK:
  828. case ACPI_NOTIFY_DEVICE_CHECK:
  829. break;
  830. case ACPI_NOTIFY_EJECT_REQUEST:
  831. ost_code = ACPI_OST_SC_EJECT_IN_PROGRESS;
  832. acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
  833. break;
  834. case ACPI_NOTIFY_DEVICE_WAKE:
  835. return;
  836. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  837. acpi_handle_err(handle, "Device cannot be configured due "
  838. "to a frequency mismatch\n");
  839. goto out;
  840. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  841. acpi_handle_err(handle, "Device cannot be configured due "
  842. "to a bus mode mismatch\n");
  843. goto out;
  844. case ACPI_NOTIFY_POWER_FAULT:
  845. acpi_handle_err(handle, "Device has suffered a power fault\n");
  846. goto out;
  847. default:
  848. acpi_handle_warn(handle, "Unsupported event type 0x%x\n", type);
  849. ost_code = ACPI_OST_SC_UNRECOGNIZED_NOTIFY;
  850. goto out;
  851. }
  852. mutex_lock(&acpiphp_context_lock);
  853. context = acpiphp_get_context(handle);
  854. if (!context || WARN_ON(context->handle != handle)
  855. || context->func.parent->is_going_away)
  856. goto err_out;
  857. get_bridge(context->func.parent);
  858. acpiphp_put_context(context);
  859. status = acpi_hotplug_execute(hotplug_event_work, context, type);
  860. if (ACPI_SUCCESS(status)) {
  861. mutex_unlock(&acpiphp_context_lock);
  862. return;
  863. }
  864. put_bridge(context->func.parent);
  865. err_out:
  866. mutex_unlock(&acpiphp_context_lock);
  867. ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
  868. out:
  869. acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
  870. }
  871. /*
  872. * Create hotplug slots for the PCI bus.
  873. * It should always return 0 to avoid skipping following notifiers.
  874. */
  875. void acpiphp_enumerate_slots(struct pci_bus *bus)
  876. {
  877. struct acpiphp_bridge *bridge;
  878. acpi_handle handle;
  879. acpi_status status;
  880. if (acpiphp_disabled)
  881. return;
  882. handle = ACPI_HANDLE(bus->bridge);
  883. if (!handle)
  884. return;
  885. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  886. if (!bridge) {
  887. acpi_handle_err(handle, "No memory for bridge object\n");
  888. return;
  889. }
  890. INIT_LIST_HEAD(&bridge->slots);
  891. kref_init(&bridge->ref);
  892. bridge->pci_dev = pci_dev_get(bus->self);
  893. bridge->pci_bus = bus;
  894. /*
  895. * Grab a ref to the subordinate PCI bus in case the bus is
  896. * removed via PCI core logical hotplug. The ref pins the bus
  897. * (which we access during module unload).
  898. */
  899. get_device(&bus->dev);
  900. if (!pci_is_root_bus(bridge->pci_bus)) {
  901. struct acpiphp_context *context;
  902. /*
  903. * This bridge should have been registered as a hotplug function
  904. * under its parent, so the context should be there, unless the
  905. * parent is going to be handled by pciehp, in which case this
  906. * bridge is not interesting to us either.
  907. */
  908. mutex_lock(&acpiphp_context_lock);
  909. context = acpiphp_get_context(handle);
  910. if (!context) {
  911. mutex_unlock(&acpiphp_context_lock);
  912. put_device(&bus->dev);
  913. pci_dev_put(bridge->pci_dev);
  914. kfree(bridge);
  915. return;
  916. }
  917. bridge->context = context;
  918. context->bridge = bridge;
  919. /* Get a reference to the parent bridge. */
  920. get_bridge(context->func.parent);
  921. mutex_unlock(&acpiphp_context_lock);
  922. }
  923. /* must be added to the list prior to calling register_slot */
  924. mutex_lock(&bridge_mutex);
  925. list_add(&bridge->list, &bridge_list);
  926. mutex_unlock(&bridge_mutex);
  927. /* register all slot objects under this bridge */
  928. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  929. register_slot, NULL, bridge, NULL);
  930. if (ACPI_FAILURE(status)) {
  931. acpi_handle_err(handle, "failed to register slots\n");
  932. cleanup_bridge(bridge);
  933. put_bridge(bridge);
  934. }
  935. }
  936. /* Destroy hotplug slots associated with the PCI bus */
  937. void acpiphp_remove_slots(struct pci_bus *bus)
  938. {
  939. struct acpiphp_bridge *bridge;
  940. if (acpiphp_disabled)
  941. return;
  942. mutex_lock(&bridge_mutex);
  943. list_for_each_entry(bridge, &bridge_list, list)
  944. if (bridge->pci_bus == bus) {
  945. mutex_unlock(&bridge_mutex);
  946. cleanup_bridge(bridge);
  947. put_bridge(bridge);
  948. return;
  949. }
  950. mutex_unlock(&bridge_mutex);
  951. }
  952. /**
  953. * acpiphp_enable_slot - power on slot
  954. * @slot: ACPI PHP slot
  955. */
  956. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  957. {
  958. pci_lock_rescan_remove();
  959. if (slot->flags & SLOT_IS_GOING_AWAY)
  960. return -ENODEV;
  961. mutex_lock(&slot->crit_sect);
  962. /* configure all functions */
  963. if (!(slot->flags & SLOT_ENABLED))
  964. enable_slot(slot);
  965. mutex_unlock(&slot->crit_sect);
  966. pci_unlock_rescan_remove();
  967. return 0;
  968. }
  969. /**
  970. * acpiphp_disable_and_eject_slot - power off and eject slot
  971. * @slot: ACPI PHP slot
  972. */
  973. static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
  974. {
  975. struct acpiphp_func *func;
  976. if (slot->flags & SLOT_IS_GOING_AWAY)
  977. return -ENODEV;
  978. mutex_lock(&slot->crit_sect);
  979. /* unconfigure all functions */
  980. disable_slot(slot);
  981. list_for_each_entry(func, &slot->funcs, sibling)
  982. if (func->flags & FUNC_HAS_EJ0) {
  983. acpi_handle handle = func_to_handle(func);
  984. if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
  985. acpi_handle_err(handle, "_EJ0 failed\n");
  986. break;
  987. }
  988. mutex_unlock(&slot->crit_sect);
  989. return 0;
  990. }
  991. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  992. {
  993. int ret;
  994. pci_lock_rescan_remove();
  995. ret = acpiphp_disable_and_eject_slot(slot);
  996. pci_unlock_rescan_remove();
  997. return ret;
  998. }
  999. /*
  1000. * slot enabled: 1
  1001. * slot disabled: 0
  1002. */
  1003. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1004. {
  1005. return (slot->flags & SLOT_ENABLED);
  1006. }
  1007. /*
  1008. * latch open: 1
  1009. * latch closed: 0
  1010. */
  1011. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1012. {
  1013. return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
  1014. }
  1015. /*
  1016. * adapter presence : 1
  1017. * absence : 0
  1018. */
  1019. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1020. {
  1021. return !!get_slot_status(slot);
  1022. }