acpiphp_glue.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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/module.h>
  42. #include <linux/kernel.h>
  43. #include <linux/pci.h>
  44. #include <linux/pci_hotplug.h>
  45. #include <linux/pci-acpi.h>
  46. #include <linux/pm_runtime.h>
  47. #include <linux/mutex.h>
  48. #include <linux/slab.h>
  49. #include <linux/acpi.h>
  50. #include "../pci.h"
  51. #include "acpiphp.h"
  52. static LIST_HEAD(bridge_list);
  53. static DEFINE_MUTEX(bridge_mutex);
  54. static int acpiphp_hotplug_notify(struct acpi_device *adev, u32 type);
  55. static void acpiphp_post_dock_fixup(struct acpi_device *adev);
  56. static void acpiphp_sanitize_bus(struct pci_bus *bus);
  57. static void acpiphp_set_hpp_values(struct pci_bus *bus);
  58. static void hotplug_event(u32 type, struct acpiphp_context *context);
  59. static void free_bridge(struct kref *kref);
  60. /**
  61. * acpiphp_init_context - Create hotplug context and grab a reference to it.
  62. * @adev: ACPI device object to create the context for.
  63. *
  64. * Call under acpi_hp_context_lock.
  65. */
  66. static struct acpiphp_context *acpiphp_init_context(struct acpi_device *adev)
  67. {
  68. struct acpiphp_context *context;
  69. context = kzalloc(sizeof(*context), GFP_KERNEL);
  70. if (!context)
  71. return NULL;
  72. context->refcount = 1;
  73. acpi_set_hp_context(adev, &context->hp, acpiphp_hotplug_notify, NULL,
  74. acpiphp_post_dock_fixup);
  75. return context;
  76. }
  77. /**
  78. * acpiphp_get_context - Get hotplug context and grab a reference to it.
  79. * @adev: ACPI device object to get the context for.
  80. *
  81. * Call under acpi_hp_context_lock.
  82. */
  83. static struct acpiphp_context *acpiphp_get_context(struct acpi_device *adev)
  84. {
  85. struct acpiphp_context *context;
  86. if (!adev->hp)
  87. return NULL;
  88. context = to_acpiphp_context(adev->hp);
  89. context->refcount++;
  90. return context;
  91. }
  92. /**
  93. * acpiphp_put_context - Drop a reference to ACPI hotplug context.
  94. * @context: ACPI hotplug context to drop a reference to.
  95. *
  96. * The context object is removed if there are no more references to it.
  97. *
  98. * Call under acpi_hp_context_lock.
  99. */
  100. static void acpiphp_put_context(struct acpiphp_context *context)
  101. {
  102. if (--context->refcount)
  103. return;
  104. WARN_ON(context->bridge);
  105. context->hp.self->hp = NULL;
  106. kfree(context);
  107. }
  108. static inline void get_bridge(struct acpiphp_bridge *bridge)
  109. {
  110. kref_get(&bridge->ref);
  111. }
  112. static inline void put_bridge(struct acpiphp_bridge *bridge)
  113. {
  114. kref_put(&bridge->ref, free_bridge);
  115. }
  116. static struct acpiphp_context *acpiphp_grab_context(struct acpi_device *adev)
  117. {
  118. struct acpiphp_context *context;
  119. acpi_lock_hp_context();
  120. context = acpiphp_get_context(adev);
  121. if (!context || context->func.parent->is_going_away) {
  122. acpi_unlock_hp_context();
  123. return NULL;
  124. }
  125. get_bridge(context->func.parent);
  126. acpiphp_put_context(context);
  127. acpi_unlock_hp_context();
  128. return context;
  129. }
  130. static void acpiphp_let_context_go(struct acpiphp_context *context)
  131. {
  132. put_bridge(context->func.parent);
  133. }
  134. static void free_bridge(struct kref *kref)
  135. {
  136. struct acpiphp_context *context;
  137. struct acpiphp_bridge *bridge;
  138. struct acpiphp_slot *slot, *next;
  139. struct acpiphp_func *func, *tmp;
  140. acpi_lock_hp_context();
  141. bridge = container_of(kref, struct acpiphp_bridge, ref);
  142. list_for_each_entry_safe(slot, next, &bridge->slots, node) {
  143. list_for_each_entry_safe(func, tmp, &slot->funcs, sibling)
  144. acpiphp_put_context(func_to_context(func));
  145. kfree(slot);
  146. }
  147. context = bridge->context;
  148. /* Root bridges will not have hotplug context. */
  149. if (context) {
  150. /* Release the reference taken by acpiphp_enumerate_slots(). */
  151. put_bridge(context->func.parent);
  152. context->bridge = NULL;
  153. acpiphp_put_context(context);
  154. }
  155. put_device(&bridge->pci_bus->dev);
  156. pci_dev_put(bridge->pci_dev);
  157. kfree(bridge);
  158. acpi_unlock_hp_context();
  159. }
  160. /**
  161. * acpiphp_post_dock_fixup - Post-dock fixups for PCI devices.
  162. * @adev: ACPI device object corresponding to a PCI device.
  163. *
  164. * TBD - figure out a way to only call fixups for systems that require them.
  165. */
  166. static void acpiphp_post_dock_fixup(struct acpi_device *adev)
  167. {
  168. struct acpiphp_context *context = acpiphp_grab_context(adev);
  169. struct pci_bus *bus;
  170. u32 buses;
  171. if (!context)
  172. return;
  173. bus = context->func.slot->bus;
  174. if (!bus->self)
  175. goto out;
  176. /* fixup bad _DCK function that rewrites
  177. * secondary bridge on slot
  178. */
  179. pci_read_config_dword(bus->self, PCI_PRIMARY_BUS, &buses);
  180. if (((buses >> 8) & 0xff) != bus->busn_res.start) {
  181. buses = (buses & 0xff000000)
  182. | ((unsigned int)(bus->primary) << 0)
  183. | ((unsigned int)(bus->busn_res.start) << 8)
  184. | ((unsigned int)(bus->busn_res.end) << 16);
  185. pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
  186. }
  187. out:
  188. acpiphp_let_context_go(context);
  189. }
  190. /* Check whether the PCI device is managed by native PCIe hotplug driver */
  191. static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
  192. {
  193. u32 reg32;
  194. acpi_handle tmp;
  195. struct acpi_pci_root *root;
  196. /* Check whether the PCIe port supports native PCIe hotplug */
  197. if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
  198. return false;
  199. if (!(reg32 & PCI_EXP_SLTCAP_HPC))
  200. return false;
  201. /*
  202. * Check whether native PCIe hotplug has been enabled for
  203. * this PCIe hierarchy.
  204. */
  205. tmp = acpi_find_root_bridge_handle(pdev);
  206. if (!tmp)
  207. return false;
  208. root = acpi_pci_find_root(tmp);
  209. if (!root)
  210. return false;
  211. if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
  212. return false;
  213. return true;
  214. }
  215. /**
  216. * acpiphp_add_context - Add ACPIPHP context to an ACPI device object.
  217. * @handle: ACPI handle of the object to add a context to.
  218. * @lvl: Not used.
  219. * @data: The object's parent ACPIPHP bridge.
  220. * @rv: Not used.
  221. */
  222. static acpi_status acpiphp_add_context(acpi_handle handle, u32 lvl, void *data,
  223. void **rv)
  224. {
  225. struct acpiphp_bridge *bridge = data;
  226. struct acpiphp_context *context;
  227. struct acpi_device *adev;
  228. struct acpiphp_slot *slot;
  229. struct acpiphp_func *newfunc;
  230. acpi_status status = AE_OK;
  231. unsigned long long adr;
  232. int device, function;
  233. struct pci_bus *pbus = bridge->pci_bus;
  234. struct pci_dev *pdev = bridge->pci_dev;
  235. u32 val;
  236. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  237. if (ACPI_FAILURE(status)) {
  238. if (status != AE_NOT_FOUND)
  239. acpi_handle_warn(handle,
  240. "can't evaluate _ADR (%#x)\n", status);
  241. return AE_OK;
  242. }
  243. if (acpi_bus_get_device(handle, &adev))
  244. return AE_OK;
  245. device = (adr >> 16) & 0xffff;
  246. function = adr & 0xffff;
  247. acpi_lock_hp_context();
  248. context = acpiphp_init_context(adev);
  249. if (!context) {
  250. acpi_unlock_hp_context();
  251. acpi_handle_err(handle, "No hotplug context\n");
  252. return AE_NOT_EXIST;
  253. }
  254. newfunc = &context->func;
  255. newfunc->function = function;
  256. newfunc->parent = bridge;
  257. acpi_unlock_hp_context();
  258. /*
  259. * If this is a dock device, its _EJ0 should be executed by the dock
  260. * notify handler after calling _DCK.
  261. */
  262. if (!is_dock_device(adev) && acpi_has_method(handle, "_EJ0"))
  263. newfunc->flags = FUNC_HAS_EJ0;
  264. if (acpi_has_method(handle, "_STA"))
  265. newfunc->flags |= FUNC_HAS_STA;
  266. /* search for objects that share the same slot */
  267. list_for_each_entry(slot, &bridge->slots, node)
  268. if (slot->device == device)
  269. goto slot_found;
  270. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  271. if (!slot) {
  272. acpi_lock_hp_context();
  273. acpiphp_put_context(context);
  274. acpi_unlock_hp_context();
  275. return AE_NO_MEMORY;
  276. }
  277. slot->bus = bridge->pci_bus;
  278. slot->device = device;
  279. INIT_LIST_HEAD(&slot->funcs);
  280. list_add_tail(&slot->node, &bridge->slots);
  281. /*
  282. * Expose slots to user space for functions that have _EJ0 or _RMV or
  283. * are located in dock stations. Do not expose them for devices handled
  284. * by the native PCIe hotplug (PCIeHP), becuase that code is supposed to
  285. * expose slots to user space in those cases.
  286. */
  287. if ((acpi_pci_check_ejectable(pbus, handle) || is_dock_device(adev))
  288. && !(pdev && device_is_managed_by_native_pciehp(pdev))) {
  289. unsigned long long sun;
  290. int retval;
  291. bridge->nr_slots++;
  292. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  293. if (ACPI_FAILURE(status))
  294. sun = bridge->nr_slots;
  295. pr_debug("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
  296. sun, pci_domain_nr(pbus), pbus->number, device);
  297. retval = acpiphp_register_hotplug_slot(slot, sun);
  298. if (retval) {
  299. slot->slot = NULL;
  300. bridge->nr_slots--;
  301. if (retval == -EBUSY)
  302. pr_warn("Slot %llu already registered by another hotplug driver\n", sun);
  303. else
  304. pr_warn("acpiphp_register_hotplug_slot failed (err code = 0x%x)\n", retval);
  305. }
  306. /* Even if the slot registration fails, we can still use it. */
  307. }
  308. slot_found:
  309. newfunc->slot = slot;
  310. list_add_tail(&newfunc->sibling, &slot->funcs);
  311. if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
  312. &val, 60*1000))
  313. slot->flags |= SLOT_ENABLED;
  314. return AE_OK;
  315. }
  316. static struct acpiphp_bridge *acpiphp_dev_to_bridge(struct acpi_device *adev)
  317. {
  318. struct acpiphp_bridge *bridge = NULL;
  319. acpi_lock_hp_context();
  320. if (adev->hp) {
  321. bridge = to_acpiphp_root_context(adev->hp)->root_bridge;
  322. if (bridge)
  323. get_bridge(bridge);
  324. }
  325. acpi_unlock_hp_context();
  326. return bridge;
  327. }
  328. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  329. {
  330. struct acpiphp_slot *slot;
  331. struct acpiphp_func *func;
  332. list_for_each_entry(slot, &bridge->slots, node) {
  333. list_for_each_entry(func, &slot->funcs, sibling) {
  334. struct acpi_device *adev = func_to_acpi_device(func);
  335. acpi_lock_hp_context();
  336. adev->hp->notify = NULL;
  337. adev->hp->fixup = NULL;
  338. acpi_unlock_hp_context();
  339. }
  340. slot->flags |= SLOT_IS_GOING_AWAY;
  341. if (slot->slot)
  342. acpiphp_unregister_hotplug_slot(slot);
  343. }
  344. mutex_lock(&bridge_mutex);
  345. list_del(&bridge->list);
  346. mutex_unlock(&bridge_mutex);
  347. acpi_lock_hp_context();
  348. bridge->is_going_away = true;
  349. acpi_unlock_hp_context();
  350. }
  351. /**
  352. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  353. * @bus: bus to start search with
  354. */
  355. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  356. {
  357. struct pci_bus *tmp;
  358. unsigned char max, n;
  359. /*
  360. * pci_bus_max_busnr will return the highest
  361. * reserved busnr for all these children.
  362. * that is equivalent to the bus->subordinate
  363. * value. We don't want to use the parent's
  364. * bus->subordinate value because it could have
  365. * padding in it.
  366. */
  367. max = bus->busn_res.start;
  368. list_for_each_entry(tmp, &bus->children, node) {
  369. n = pci_bus_max_busnr(tmp);
  370. if (n > max)
  371. max = n;
  372. }
  373. return max;
  374. }
  375. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  376. {
  377. struct acpiphp_func *func;
  378. union acpi_object params[2];
  379. struct acpi_object_list arg_list;
  380. list_for_each_entry(func, &slot->funcs, sibling) {
  381. arg_list.count = 2;
  382. arg_list.pointer = params;
  383. params[0].type = ACPI_TYPE_INTEGER;
  384. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  385. params[1].type = ACPI_TYPE_INTEGER;
  386. params[1].integer.value = 1;
  387. /* _REG is optional, we don't care about if there is failure */
  388. acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
  389. NULL);
  390. }
  391. }
  392. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  393. {
  394. struct acpiphp_func *func;
  395. /* quirk, or pcie could set it already */
  396. if (dev->is_hotplug_bridge)
  397. return;
  398. list_for_each_entry(func, &slot->funcs, sibling) {
  399. if (PCI_FUNC(dev->devfn) == func->function) {
  400. dev->is_hotplug_bridge = 1;
  401. break;
  402. }
  403. }
  404. }
  405. static int acpiphp_rescan_slot(struct acpiphp_slot *slot)
  406. {
  407. struct acpiphp_func *func;
  408. list_for_each_entry(func, &slot->funcs, sibling) {
  409. struct acpi_device *adev = func_to_acpi_device(func);
  410. acpi_bus_scan(adev->handle);
  411. if (acpi_device_enumerated(adev))
  412. acpi_device_set_power(adev, ACPI_STATE_D0);
  413. }
  414. return pci_scan_slot(slot->bus, PCI_DEVFN(slot->device, 0));
  415. }
  416. /**
  417. * enable_slot - enable, configure a slot
  418. * @slot: slot to be enabled
  419. *
  420. * This function should be called per *physical slot*,
  421. * not per each slot object in ACPI namespace.
  422. */
  423. static void enable_slot(struct acpiphp_slot *slot)
  424. {
  425. struct pci_dev *dev;
  426. struct pci_bus *bus = slot->bus;
  427. struct acpiphp_func *func;
  428. int max, pass;
  429. LIST_HEAD(add_list);
  430. acpiphp_rescan_slot(slot);
  431. max = acpiphp_max_busnr(bus);
  432. for (pass = 0; pass < 2; pass++) {
  433. list_for_each_entry(dev, &bus->devices, bus_list) {
  434. if (PCI_SLOT(dev->devfn) != slot->device)
  435. continue;
  436. if (pci_is_bridge(dev)) {
  437. max = pci_scan_bridge(bus, dev, max, pass);
  438. if (pass && dev->subordinate) {
  439. check_hotplug_bridge(slot, dev);
  440. pcibios_resource_survey_bus(dev->subordinate);
  441. __pci_bus_size_bridges(dev->subordinate,
  442. &add_list);
  443. }
  444. }
  445. }
  446. }
  447. __pci_bus_assign_resources(bus, &add_list, NULL);
  448. acpiphp_sanitize_bus(bus);
  449. acpiphp_set_hpp_values(bus);
  450. acpiphp_set_acpi_region(slot);
  451. list_for_each_entry(dev, &bus->devices, bus_list) {
  452. /* Assume that newly added devices are powered on already. */
  453. if (!dev->is_added)
  454. dev->current_state = PCI_D0;
  455. }
  456. pci_bus_add_devices(bus);
  457. slot->flags |= SLOT_ENABLED;
  458. list_for_each_entry(func, &slot->funcs, sibling) {
  459. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  460. func->function));
  461. if (!dev) {
  462. /* Do not set SLOT_ENABLED flag if some funcs
  463. are not added. */
  464. slot->flags &= (~SLOT_ENABLED);
  465. continue;
  466. }
  467. }
  468. }
  469. /**
  470. * disable_slot - disable a slot
  471. * @slot: ACPI PHP slot
  472. */
  473. static void disable_slot(struct acpiphp_slot *slot)
  474. {
  475. struct pci_bus *bus = slot->bus;
  476. struct pci_dev *dev, *prev;
  477. struct acpiphp_func *func;
  478. /*
  479. * enable_slot() enumerates all functions in this device via
  480. * pci_scan_slot(), whether they have associated ACPI hotplug
  481. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  482. * here.
  483. */
  484. list_for_each_entry_safe_reverse(dev, prev, &bus->devices, bus_list)
  485. if (PCI_SLOT(dev->devfn) == slot->device)
  486. pci_stop_and_remove_bus_device(dev);
  487. list_for_each_entry(func, &slot->funcs, sibling)
  488. acpi_bus_trim(func_to_acpi_device(func));
  489. slot->flags &= (~SLOT_ENABLED);
  490. }
  491. static bool acpiphp_no_hotplug(struct acpi_device *adev)
  492. {
  493. return adev && adev->flags.no_hotplug;
  494. }
  495. static bool slot_no_hotplug(struct acpiphp_slot *slot)
  496. {
  497. struct acpiphp_func *func;
  498. list_for_each_entry(func, &slot->funcs, sibling)
  499. if (acpiphp_no_hotplug(func_to_acpi_device(func)))
  500. return true;
  501. return false;
  502. }
  503. /**
  504. * get_slot_status - get ACPI slot status
  505. * @slot: ACPI PHP slot
  506. *
  507. * If a slot has _STA for each function and if any one of them
  508. * returned non-zero status, return it.
  509. *
  510. * If a slot doesn't have _STA and if any one of its functions'
  511. * configuration space is configured, return 0x0f as a _STA.
  512. *
  513. * Otherwise return 0.
  514. */
  515. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  516. {
  517. unsigned long long sta = 0;
  518. struct acpiphp_func *func;
  519. list_for_each_entry(func, &slot->funcs, sibling) {
  520. if (func->flags & FUNC_HAS_STA) {
  521. acpi_status status;
  522. status = acpi_evaluate_integer(func_to_handle(func),
  523. "_STA", NULL, &sta);
  524. if (ACPI_SUCCESS(status) && sta)
  525. break;
  526. } else {
  527. u32 dvid;
  528. pci_bus_read_config_dword(slot->bus,
  529. PCI_DEVFN(slot->device,
  530. func->function),
  531. PCI_VENDOR_ID, &dvid);
  532. if (dvid != 0xffffffff) {
  533. sta = ACPI_STA_ALL;
  534. break;
  535. }
  536. }
  537. }
  538. return (unsigned int)sta;
  539. }
  540. static inline bool device_status_valid(unsigned int sta)
  541. {
  542. /*
  543. * ACPI spec says that _STA may return bit 0 clear with bit 3 set
  544. * if the device is valid but does not require a device driver to be
  545. * loaded (Section 6.3.7 of ACPI 5.0A).
  546. */
  547. unsigned int mask = ACPI_STA_DEVICE_ENABLED | ACPI_STA_DEVICE_FUNCTIONING;
  548. return (sta & mask) == mask;
  549. }
  550. /**
  551. * trim_stale_devices - remove PCI devices that are not responding.
  552. * @dev: PCI device to start walking the hierarchy from.
  553. */
  554. static void trim_stale_devices(struct pci_dev *dev)
  555. {
  556. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  557. struct pci_bus *bus = dev->subordinate;
  558. bool alive = false;
  559. if (adev) {
  560. acpi_status status;
  561. unsigned long long sta;
  562. status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
  563. alive = (ACPI_SUCCESS(status) && device_status_valid(sta))
  564. || acpiphp_no_hotplug(adev);
  565. }
  566. if (!alive)
  567. alive = pci_device_is_present(dev);
  568. if (!alive) {
  569. pci_stop_and_remove_bus_device(dev);
  570. if (adev)
  571. acpi_bus_trim(adev);
  572. } else if (bus) {
  573. struct pci_dev *child, *tmp;
  574. /* The device is a bridge. so check the bus below it. */
  575. pm_runtime_get_sync(&dev->dev);
  576. list_for_each_entry_safe_reverse(child, tmp, &bus->devices, bus_list)
  577. trim_stale_devices(child);
  578. pm_runtime_put(&dev->dev);
  579. }
  580. }
  581. /**
  582. * acpiphp_check_bridge - re-enumerate devices
  583. * @bridge: where to begin re-enumeration
  584. *
  585. * Iterate over all slots under this bridge and make sure that if a
  586. * card is present they are enabled, and if not they are disabled.
  587. */
  588. static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  589. {
  590. struct acpiphp_slot *slot;
  591. /* Bail out if the bridge is going away. */
  592. if (bridge->is_going_away)
  593. return;
  594. list_for_each_entry(slot, &bridge->slots, node) {
  595. struct pci_bus *bus = slot->bus;
  596. struct pci_dev *dev, *tmp;
  597. if (slot_no_hotplug(slot)) {
  598. ; /* do nothing */
  599. } else if (device_status_valid(get_slot_status(slot))) {
  600. /* remove stale devices if any */
  601. list_for_each_entry_safe_reverse(dev, tmp,
  602. &bus->devices, bus_list)
  603. if (PCI_SLOT(dev->devfn) == slot->device)
  604. trim_stale_devices(dev);
  605. /* configure all functions */
  606. enable_slot(slot);
  607. } else {
  608. disable_slot(slot);
  609. }
  610. }
  611. }
  612. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  613. {
  614. struct pci_dev *dev;
  615. list_for_each_entry(dev, &bus->devices, bus_list)
  616. pci_configure_slot(dev);
  617. }
  618. /*
  619. * Remove devices for which we could not assign resources, call
  620. * arch specific code to fix-up the bus
  621. */
  622. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  623. {
  624. struct pci_dev *dev, *tmp;
  625. int i;
  626. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  627. list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
  628. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  629. struct resource *res = &dev->resource[i];
  630. if ((res->flags & type_mask) && !res->start &&
  631. res->end) {
  632. /* Could not assign a required resources
  633. * for this device, remove it */
  634. pci_stop_and_remove_bus_device(dev);
  635. break;
  636. }
  637. }
  638. }
  639. }
  640. /*
  641. * ACPI event handlers
  642. */
  643. void acpiphp_check_host_bridge(struct acpi_device *adev)
  644. {
  645. struct acpiphp_bridge *bridge;
  646. bridge = acpiphp_dev_to_bridge(adev);
  647. if (bridge) {
  648. pci_lock_rescan_remove();
  649. acpiphp_check_bridge(bridge);
  650. pci_unlock_rescan_remove();
  651. put_bridge(bridge);
  652. }
  653. }
  654. static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot);
  655. static void hotplug_event(u32 type, struct acpiphp_context *context)
  656. {
  657. acpi_handle handle = context->hp.self->handle;
  658. struct acpiphp_func *func = &context->func;
  659. struct acpiphp_slot *slot = func->slot;
  660. struct acpiphp_bridge *bridge;
  661. acpi_lock_hp_context();
  662. bridge = context->bridge;
  663. if (bridge)
  664. get_bridge(bridge);
  665. acpi_unlock_hp_context();
  666. pci_lock_rescan_remove();
  667. switch (type) {
  668. case ACPI_NOTIFY_BUS_CHECK:
  669. /* bus re-enumerate */
  670. acpi_handle_debug(handle, "Bus check in %s()\n", __func__);
  671. if (bridge)
  672. acpiphp_check_bridge(bridge);
  673. else if (!(slot->flags & SLOT_IS_GOING_AWAY))
  674. enable_slot(slot);
  675. break;
  676. case ACPI_NOTIFY_DEVICE_CHECK:
  677. /* device check */
  678. acpi_handle_debug(handle, "Device check in %s()\n", __func__);
  679. if (bridge) {
  680. acpiphp_check_bridge(bridge);
  681. } else if (!(slot->flags & SLOT_IS_GOING_AWAY)) {
  682. /*
  683. * Check if anything has changed in the slot and rescan
  684. * from the parent if that's the case.
  685. */
  686. if (acpiphp_rescan_slot(slot))
  687. acpiphp_check_bridge(func->parent);
  688. }
  689. break;
  690. case ACPI_NOTIFY_EJECT_REQUEST:
  691. /* request device eject */
  692. acpi_handle_debug(handle, "Eject request in %s()\n", __func__);
  693. acpiphp_disable_and_eject_slot(slot);
  694. break;
  695. }
  696. pci_unlock_rescan_remove();
  697. if (bridge)
  698. put_bridge(bridge);
  699. }
  700. static int acpiphp_hotplug_notify(struct acpi_device *adev, u32 type)
  701. {
  702. struct acpiphp_context *context;
  703. context = acpiphp_grab_context(adev);
  704. if (!context)
  705. return -ENODATA;
  706. hotplug_event(type, context);
  707. acpiphp_let_context_go(context);
  708. return 0;
  709. }
  710. /**
  711. * acpiphp_enumerate_slots - Enumerate PCI slots for a given bus.
  712. * @bus: PCI bus to enumerate the slots for.
  713. *
  714. * A "slot" is an object associated with a PCI device number. All functions
  715. * (PCI devices) with the same bus and device number belong to the same slot.
  716. */
  717. void acpiphp_enumerate_slots(struct pci_bus *bus)
  718. {
  719. struct acpiphp_bridge *bridge;
  720. struct acpi_device *adev;
  721. acpi_handle handle;
  722. acpi_status status;
  723. if (acpiphp_disabled)
  724. return;
  725. adev = ACPI_COMPANION(bus->bridge);
  726. if (!adev)
  727. return;
  728. handle = adev->handle;
  729. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  730. if (!bridge) {
  731. acpi_handle_err(handle, "No memory for bridge object\n");
  732. return;
  733. }
  734. INIT_LIST_HEAD(&bridge->slots);
  735. kref_init(&bridge->ref);
  736. bridge->pci_dev = pci_dev_get(bus->self);
  737. bridge->pci_bus = bus;
  738. /*
  739. * Grab a ref to the subordinate PCI bus in case the bus is
  740. * removed via PCI core logical hotplug. The ref pins the bus
  741. * (which we access during module unload).
  742. */
  743. get_device(&bus->dev);
  744. acpi_lock_hp_context();
  745. if (pci_is_root_bus(bridge->pci_bus)) {
  746. struct acpiphp_root_context *root_context;
  747. root_context = kzalloc(sizeof(*root_context), GFP_KERNEL);
  748. if (!root_context)
  749. goto err;
  750. root_context->root_bridge = bridge;
  751. acpi_set_hp_context(adev, &root_context->hp, NULL, NULL, NULL);
  752. } else {
  753. struct acpiphp_context *context;
  754. /*
  755. * This bridge should have been registered as a hotplug function
  756. * under its parent, so the context should be there, unless the
  757. * parent is going to be handled by pciehp, in which case this
  758. * bridge is not interesting to us either.
  759. */
  760. context = acpiphp_get_context(adev);
  761. if (!context)
  762. goto err;
  763. bridge->context = context;
  764. context->bridge = bridge;
  765. /* Get a reference to the parent bridge. */
  766. get_bridge(context->func.parent);
  767. }
  768. acpi_unlock_hp_context();
  769. /* Must be added to the list prior to calling acpiphp_add_context(). */
  770. mutex_lock(&bridge_mutex);
  771. list_add(&bridge->list, &bridge_list);
  772. mutex_unlock(&bridge_mutex);
  773. /* register all slot objects under this bridge */
  774. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  775. acpiphp_add_context, NULL, bridge, NULL);
  776. if (ACPI_FAILURE(status)) {
  777. acpi_handle_err(handle, "failed to register slots\n");
  778. cleanup_bridge(bridge);
  779. put_bridge(bridge);
  780. }
  781. return;
  782. err:
  783. acpi_unlock_hp_context();
  784. put_device(&bus->dev);
  785. pci_dev_put(bridge->pci_dev);
  786. kfree(bridge);
  787. }
  788. void acpiphp_drop_bridge(struct acpiphp_bridge *bridge)
  789. {
  790. if (pci_is_root_bus(bridge->pci_bus)) {
  791. struct acpiphp_root_context *root_context;
  792. struct acpi_device *adev;
  793. acpi_lock_hp_context();
  794. adev = ACPI_COMPANION(bridge->pci_bus->bridge);
  795. root_context = to_acpiphp_root_context(adev->hp);
  796. adev->hp = NULL;
  797. acpi_unlock_hp_context();
  798. kfree(root_context);
  799. }
  800. cleanup_bridge(bridge);
  801. put_bridge(bridge);
  802. }
  803. /**
  804. * acpiphp_remove_slots - Remove slot objects associated with a given bus.
  805. * @bus: PCI bus to remove the slot objects for.
  806. */
  807. void acpiphp_remove_slots(struct pci_bus *bus)
  808. {
  809. struct acpiphp_bridge *bridge;
  810. if (acpiphp_disabled)
  811. return;
  812. mutex_lock(&bridge_mutex);
  813. list_for_each_entry(bridge, &bridge_list, list)
  814. if (bridge->pci_bus == bus) {
  815. mutex_unlock(&bridge_mutex);
  816. acpiphp_drop_bridge(bridge);
  817. return;
  818. }
  819. mutex_unlock(&bridge_mutex);
  820. }
  821. /**
  822. * acpiphp_enable_slot - power on slot
  823. * @slot: ACPI PHP slot
  824. */
  825. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  826. {
  827. pci_lock_rescan_remove();
  828. if (slot->flags & SLOT_IS_GOING_AWAY)
  829. return -ENODEV;
  830. /* configure all functions */
  831. if (!(slot->flags & SLOT_ENABLED))
  832. enable_slot(slot);
  833. pci_unlock_rescan_remove();
  834. return 0;
  835. }
  836. /**
  837. * acpiphp_disable_and_eject_slot - power off and eject slot
  838. * @slot: ACPI PHP slot
  839. */
  840. static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
  841. {
  842. struct acpiphp_func *func;
  843. if (slot->flags & SLOT_IS_GOING_AWAY)
  844. return -ENODEV;
  845. /* unconfigure all functions */
  846. disable_slot(slot);
  847. list_for_each_entry(func, &slot->funcs, sibling)
  848. if (func->flags & FUNC_HAS_EJ0) {
  849. acpi_handle handle = func_to_handle(func);
  850. if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
  851. acpi_handle_err(handle, "_EJ0 failed\n");
  852. break;
  853. }
  854. return 0;
  855. }
  856. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  857. {
  858. int ret;
  859. /*
  860. * Acquire acpi_scan_lock to ensure that the execution of _EJ0 in
  861. * acpiphp_disable_and_eject_slot() will be synchronized properly.
  862. */
  863. acpi_scan_lock_acquire();
  864. pci_lock_rescan_remove();
  865. ret = acpiphp_disable_and_eject_slot(slot);
  866. pci_unlock_rescan_remove();
  867. acpi_scan_lock_release();
  868. return ret;
  869. }
  870. /*
  871. * slot enabled: 1
  872. * slot disabled: 0
  873. */
  874. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  875. {
  876. return (slot->flags & SLOT_ENABLED);
  877. }
  878. /*
  879. * latch open: 1
  880. * latch closed: 0
  881. */
  882. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  883. {
  884. return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
  885. }
  886. /*
  887. * adapter presence : 1
  888. * absence : 0
  889. */
  890. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  891. {
  892. return !!get_slot_status(slot);
  893. }