acpiphp_glue.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  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. #include <linux/init.h>
  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/mutex.h>
  47. #include "../pci.h"
  48. #include "acpiphp.h"
  49. static LIST_HEAD(bridge_list);
  50. static LIST_HEAD(ioapic_list);
  51. static DEFINE_SPINLOCK(ioapic_list_lock);
  52. #define MY_NAME "acpiphp_glue"
  53. static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
  54. static void acpiphp_sanitize_bus(struct pci_bus *bus);
  55. static void acpiphp_set_hpp_values(struct pci_bus *bus);
  56. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
  57. /* callback routine to check for the existence of a pci dock device */
  58. static acpi_status
  59. is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
  60. {
  61. int *count = (int *)context;
  62. if (is_dock_device(handle)) {
  63. (*count)++;
  64. return AE_CTRL_TERMINATE;
  65. } else {
  66. return AE_OK;
  67. }
  68. }
  69. /*
  70. * the _DCK method can do funny things... and sometimes not
  71. * hah-hah funny.
  72. *
  73. * TBD - figure out a way to only call fixups for
  74. * systems that require them.
  75. */
  76. static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
  77. void *v)
  78. {
  79. struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
  80. struct pci_bus *bus = func->slot->bridge->pci_bus;
  81. u32 buses;
  82. if (!bus->self)
  83. return NOTIFY_OK;
  84. /* fixup bad _DCK function that rewrites
  85. * secondary bridge on slot
  86. */
  87. pci_read_config_dword(bus->self,
  88. PCI_PRIMARY_BUS,
  89. &buses);
  90. if (((buses >> 8) & 0xff) != bus->secondary) {
  91. buses = (buses & 0xff000000)
  92. | ((unsigned int)(bus->primary) << 0)
  93. | ((unsigned int)(bus->secondary) << 8)
  94. | ((unsigned int)(bus->subordinate) << 16);
  95. pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
  96. }
  97. return NOTIFY_OK;
  98. }
  99. static struct acpi_dock_ops acpiphp_dock_ops = {
  100. .handler = handle_hotplug_event_func,
  101. };
  102. /* callback routine to register each ACPI PCI slot object */
  103. static acpi_status
  104. register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
  105. {
  106. struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
  107. struct acpiphp_slot *slot;
  108. struct acpiphp_func *newfunc;
  109. acpi_handle tmp;
  110. acpi_status status = AE_OK;
  111. unsigned long long adr, sun;
  112. int device, function, retval;
  113. struct pci_bus *pbus = bridge->pci_bus;
  114. struct pci_dev *pdev;
  115. if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
  116. return AE_OK;
  117. acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  118. device = (adr >> 16) & 0xffff;
  119. function = adr & 0xffff;
  120. newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
  121. if (!newfunc)
  122. return AE_NO_MEMORY;
  123. INIT_LIST_HEAD(&newfunc->sibling);
  124. newfunc->handle = handle;
  125. newfunc->function = function;
  126. if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
  127. newfunc->flags = FUNC_HAS_EJ0;
  128. if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
  129. newfunc->flags |= FUNC_HAS_STA;
  130. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
  131. newfunc->flags |= FUNC_HAS_PS0;
  132. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
  133. newfunc->flags |= FUNC_HAS_PS3;
  134. if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
  135. newfunc->flags |= FUNC_HAS_DCK;
  136. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  137. if (ACPI_FAILURE(status)) {
  138. /*
  139. * use the count of the number of slots we've found
  140. * for the number of the slot
  141. */
  142. sun = bridge->nr_slots+1;
  143. }
  144. /* search for objects that share the same slot */
  145. for (slot = bridge->slots; slot; slot = slot->next)
  146. if (slot->device == device) {
  147. if (slot->sun != sun)
  148. warn("sibling found, but _SUN doesn't match!\n");
  149. break;
  150. }
  151. if (!slot) {
  152. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  153. if (!slot) {
  154. kfree(newfunc);
  155. return AE_NO_MEMORY;
  156. }
  157. slot->bridge = bridge;
  158. slot->device = device;
  159. slot->sun = sun;
  160. INIT_LIST_HEAD(&slot->funcs);
  161. mutex_init(&slot->crit_sect);
  162. slot->next = bridge->slots;
  163. bridge->slots = slot;
  164. bridge->nr_slots++;
  165. dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
  166. slot->sun, pci_domain_nr(pbus), pbus->number, device);
  167. retval = acpiphp_register_hotplug_slot(slot);
  168. if (retval) {
  169. if (retval == -EBUSY)
  170. warn("Slot %llu already registered by another "
  171. "hotplug driver\n", slot->sun);
  172. else
  173. warn("acpiphp_register_hotplug_slot failed "
  174. "(err code = 0x%x)\n", retval);
  175. goto err_exit;
  176. }
  177. }
  178. newfunc->slot = slot;
  179. list_add_tail(&newfunc->sibling, &slot->funcs);
  180. pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
  181. if (pdev) {
  182. slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
  183. pci_dev_put(pdev);
  184. }
  185. if (is_dock_device(handle)) {
  186. /* we don't want to call this device's _EJ0
  187. * because we want the dock notify handler
  188. * to call it after it calls _DCK
  189. */
  190. newfunc->flags &= ~FUNC_HAS_EJ0;
  191. if (register_hotplug_dock_device(handle,
  192. &acpiphp_dock_ops, newfunc))
  193. dbg("failed to register dock device\n");
  194. /* we need to be notified when dock events happen
  195. * outside of the hotplug operation, since we may
  196. * need to do fixups before we can hotplug.
  197. */
  198. newfunc->nb.notifier_call = post_dock_fixups;
  199. if (register_dock_notifier(&newfunc->nb))
  200. dbg("failed to register a dock notifier");
  201. }
  202. /* install notify handler */
  203. if (!(newfunc->flags & FUNC_HAS_DCK)) {
  204. status = acpi_install_notify_handler(handle,
  205. ACPI_SYSTEM_NOTIFY,
  206. handle_hotplug_event_func,
  207. newfunc);
  208. if (ACPI_FAILURE(status))
  209. err("failed to register interrupt notify handler\n");
  210. } else
  211. status = AE_OK;
  212. return status;
  213. err_exit:
  214. bridge->nr_slots--;
  215. bridge->slots = slot->next;
  216. kfree(slot);
  217. kfree(newfunc);
  218. return AE_OK;
  219. }
  220. /* see if it's worth looking at this bridge */
  221. static int detect_ejectable_slots(acpi_handle handle)
  222. {
  223. int found = acpi_pci_detect_ejectable(handle);
  224. if (!found) {
  225. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  226. is_pci_dock_device, (void *)&found, NULL);
  227. }
  228. return found;
  229. }
  230. /* decode ACPI 2.0 _HPP hot plug parameters */
  231. static void decode_hpp(struct pci_dev *dev, struct hotplug_params *hpp)
  232. {
  233. acpi_status status;
  234. status = acpi_get_hp_params_from_firmware(dev->bus, hpp);
  235. if (ACPI_FAILURE(status) ||
  236. !hpp->t0 || (hpp->t0->revision > 1)) {
  237. /* use default numbers */
  238. printk(KERN_WARNING
  239. "%s: Could not get hotplug parameters. Use defaults\n",
  240. __func__);
  241. hpp->t0 = &hpp->type0_data;
  242. hpp->t0->revision = 0;
  243. hpp->t0->cache_line_size = 0x10;
  244. hpp->t0->latency_timer = 0x40;
  245. hpp->t0->enable_serr = 0;
  246. hpp->t0->enable_perr = 0;
  247. }
  248. }
  249. /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
  250. static void init_bridge_misc(struct acpiphp_bridge *bridge)
  251. {
  252. acpi_status status;
  253. /* must be added to the list prior to calling register_slot */
  254. list_add(&bridge->list, &bridge_list);
  255. /* register all slot objects under this bridge */
  256. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
  257. register_slot, bridge, NULL);
  258. if (ACPI_FAILURE(status)) {
  259. list_del(&bridge->list);
  260. return;
  261. }
  262. /* install notify handler */
  263. if (bridge->type != BRIDGE_TYPE_HOST) {
  264. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  265. status = acpi_remove_notify_handler(bridge->func->handle,
  266. ACPI_SYSTEM_NOTIFY,
  267. handle_hotplug_event_func);
  268. if (ACPI_FAILURE(status))
  269. err("failed to remove notify handler\n");
  270. }
  271. status = acpi_install_notify_handler(bridge->handle,
  272. ACPI_SYSTEM_NOTIFY,
  273. handle_hotplug_event_bridge,
  274. bridge);
  275. if (ACPI_FAILURE(status)) {
  276. err("failed to register interrupt notify handler\n");
  277. }
  278. }
  279. }
  280. /* find acpiphp_func from acpiphp_bridge */
  281. static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
  282. {
  283. struct list_head *node, *l;
  284. struct acpiphp_bridge *bridge;
  285. struct acpiphp_slot *slot;
  286. struct acpiphp_func *func;
  287. list_for_each(node, &bridge_list) {
  288. bridge = list_entry(node, struct acpiphp_bridge, list);
  289. for (slot = bridge->slots; slot; slot = slot->next) {
  290. list_for_each(l, &slot->funcs) {
  291. func = list_entry(l, struct acpiphp_func,
  292. sibling);
  293. if (func->handle == handle)
  294. return func;
  295. }
  296. }
  297. }
  298. return NULL;
  299. }
  300. static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
  301. {
  302. acpi_handle dummy_handle;
  303. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  304. "_STA", &dummy_handle)))
  305. bridge->flags |= BRIDGE_HAS_STA;
  306. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  307. "_EJ0", &dummy_handle)))
  308. bridge->flags |= BRIDGE_HAS_EJ0;
  309. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  310. "_PS0", &dummy_handle)))
  311. bridge->flags |= BRIDGE_HAS_PS0;
  312. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  313. "_PS3", &dummy_handle)))
  314. bridge->flags |= BRIDGE_HAS_PS3;
  315. /* is this ejectable p2p bridge? */
  316. if (bridge->flags & BRIDGE_HAS_EJ0) {
  317. struct acpiphp_func *func;
  318. dbg("found ejectable p2p bridge\n");
  319. /* make link between PCI bridge and PCI function */
  320. func = acpiphp_bridge_handle_to_function(bridge->handle);
  321. if (!func)
  322. return;
  323. bridge->func = func;
  324. func->bridge = bridge;
  325. }
  326. }
  327. /* allocate and initialize host bridge data structure */
  328. static void add_host_bridge(acpi_handle *handle)
  329. {
  330. struct acpiphp_bridge *bridge;
  331. struct acpi_pci_root *root = acpi_pci_find_root(handle);
  332. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  333. if (bridge == NULL)
  334. return;
  335. bridge->type = BRIDGE_TYPE_HOST;
  336. bridge->handle = handle;
  337. bridge->pci_bus = root->bus;
  338. spin_lock_init(&bridge->res_lock);
  339. init_bridge_misc(bridge);
  340. }
  341. /* allocate and initialize PCI-to-PCI bridge data structure */
  342. static void add_p2p_bridge(acpi_handle *handle)
  343. {
  344. struct acpiphp_bridge *bridge;
  345. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  346. if (bridge == NULL) {
  347. err("out of memory\n");
  348. return;
  349. }
  350. bridge->type = BRIDGE_TYPE_P2P;
  351. bridge->handle = handle;
  352. config_p2p_bridge_flags(bridge);
  353. bridge->pci_dev = acpi_get_pci_dev(handle);
  354. bridge->pci_bus = bridge->pci_dev->subordinate;
  355. if (!bridge->pci_bus) {
  356. err("This is not a PCI-to-PCI bridge!\n");
  357. goto err;
  358. }
  359. /*
  360. * Grab a ref to the subordinate PCI bus in case the bus is
  361. * removed via PCI core logical hotplug. The ref pins the bus
  362. * (which we access during module unload).
  363. */
  364. get_device(&bridge->pci_bus->dev);
  365. spin_lock_init(&bridge->res_lock);
  366. init_bridge_misc(bridge);
  367. return;
  368. err:
  369. pci_dev_put(bridge->pci_dev);
  370. kfree(bridge);
  371. return;
  372. }
  373. /* callback routine to find P2P bridges */
  374. static acpi_status
  375. find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  376. {
  377. acpi_status status;
  378. struct pci_dev *dev;
  379. dev = acpi_get_pci_dev(handle);
  380. if (!dev || !dev->subordinate)
  381. goto out;
  382. /* check if this bridge has ejectable slots */
  383. if ((detect_ejectable_slots(handle) > 0)) {
  384. dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
  385. add_p2p_bridge(handle);
  386. }
  387. /* search P2P bridges under this p2p bridge */
  388. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  389. find_p2p_bridge, NULL, NULL);
  390. if (ACPI_FAILURE(status))
  391. warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
  392. out:
  393. pci_dev_put(dev);
  394. return AE_OK;
  395. }
  396. /* find hot-pluggable slots, and then find P2P bridge */
  397. static int add_bridge(acpi_handle handle)
  398. {
  399. acpi_status status;
  400. unsigned long long tmp;
  401. acpi_handle dummy_handle;
  402. /* if the bridge doesn't have _STA, we assume it is always there */
  403. status = acpi_get_handle(handle, "_STA", &dummy_handle);
  404. if (ACPI_SUCCESS(status)) {
  405. status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
  406. if (ACPI_FAILURE(status)) {
  407. dbg("%s: _STA evaluation failure\n", __func__);
  408. return 0;
  409. }
  410. if ((tmp & ACPI_STA_FUNCTIONING) == 0)
  411. /* don't register this object */
  412. return 0;
  413. }
  414. /* check if this bridge has ejectable slots */
  415. if (detect_ejectable_slots(handle) > 0) {
  416. dbg("found PCI host-bus bridge with hot-pluggable slots\n");
  417. add_host_bridge(handle);
  418. }
  419. /* search P2P bridges under this host bridge */
  420. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  421. find_p2p_bridge, NULL, NULL);
  422. if (ACPI_FAILURE(status))
  423. warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
  424. return 0;
  425. }
  426. static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
  427. {
  428. struct list_head *head;
  429. list_for_each(head, &bridge_list) {
  430. struct acpiphp_bridge *bridge = list_entry(head,
  431. struct acpiphp_bridge, list);
  432. if (bridge->handle == handle)
  433. return bridge;
  434. }
  435. return NULL;
  436. }
  437. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  438. {
  439. struct list_head *list, *tmp;
  440. struct acpiphp_slot *slot;
  441. acpi_status status;
  442. acpi_handle handle = bridge->handle;
  443. status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  444. handle_hotplug_event_bridge);
  445. if (ACPI_FAILURE(status))
  446. err("failed to remove notify handler\n");
  447. if ((bridge->type != BRIDGE_TYPE_HOST) &&
  448. ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
  449. status = acpi_install_notify_handler(bridge->func->handle,
  450. ACPI_SYSTEM_NOTIFY,
  451. handle_hotplug_event_func,
  452. bridge->func);
  453. if (ACPI_FAILURE(status))
  454. err("failed to install interrupt notify handler\n");
  455. }
  456. slot = bridge->slots;
  457. while (slot) {
  458. struct acpiphp_slot *next = slot->next;
  459. list_for_each_safe (list, tmp, &slot->funcs) {
  460. struct acpiphp_func *func;
  461. func = list_entry(list, struct acpiphp_func, sibling);
  462. if (is_dock_device(func->handle)) {
  463. unregister_hotplug_dock_device(func->handle);
  464. unregister_dock_notifier(&func->nb);
  465. }
  466. if (!(func->flags & FUNC_HAS_DCK)) {
  467. status = acpi_remove_notify_handler(func->handle,
  468. ACPI_SYSTEM_NOTIFY,
  469. handle_hotplug_event_func);
  470. if (ACPI_FAILURE(status))
  471. err("failed to remove notify handler\n");
  472. }
  473. list_del(list);
  474. kfree(func);
  475. }
  476. acpiphp_unregister_hotplug_slot(slot);
  477. list_del(&slot->funcs);
  478. kfree(slot);
  479. slot = next;
  480. }
  481. /*
  482. * Only P2P bridges have a pci_dev
  483. */
  484. if (bridge->pci_dev)
  485. put_device(&bridge->pci_bus->dev);
  486. pci_dev_put(bridge->pci_dev);
  487. list_del(&bridge->list);
  488. kfree(bridge);
  489. }
  490. static acpi_status
  491. cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  492. {
  493. struct acpiphp_bridge *bridge;
  494. /* cleanup p2p bridges under this P2P bridge
  495. in a depth-first manner */
  496. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  497. cleanup_p2p_bridge, NULL, NULL);
  498. bridge = acpiphp_handle_to_bridge(handle);
  499. if (bridge)
  500. cleanup_bridge(bridge);
  501. return AE_OK;
  502. }
  503. static void remove_bridge(acpi_handle handle)
  504. {
  505. struct acpiphp_bridge *bridge;
  506. /* cleanup p2p bridges under this host bridge
  507. in a depth-first manner */
  508. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  509. (u32)1, cleanup_p2p_bridge, NULL, NULL);
  510. /*
  511. * On root bridges with hotplug slots directly underneath (ie,
  512. * no p2p bridge inbetween), we call cleanup_bridge().
  513. *
  514. * The else clause cleans up root bridges that either had no
  515. * hotplug slots at all, or had a p2p bridge underneath.
  516. */
  517. bridge = acpiphp_handle_to_bridge(handle);
  518. if (bridge)
  519. cleanup_bridge(bridge);
  520. else
  521. acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  522. handle_hotplug_event_bridge);
  523. }
  524. static struct pci_dev * get_apic_pci_info(acpi_handle handle)
  525. {
  526. struct pci_dev *dev;
  527. dev = acpi_get_pci_dev(handle);
  528. if (!dev)
  529. return NULL;
  530. if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
  531. (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
  532. {
  533. pci_dev_put(dev);
  534. return NULL;
  535. }
  536. return dev;
  537. }
  538. static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
  539. {
  540. acpi_status status;
  541. int result = -1;
  542. unsigned long long gsb;
  543. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  544. union acpi_object *obj;
  545. void *table;
  546. status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
  547. if (ACPI_SUCCESS(status)) {
  548. *gsi_base = (u32)gsb;
  549. return 0;
  550. }
  551. status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
  552. if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
  553. return -1;
  554. obj = buffer.pointer;
  555. if (obj->type != ACPI_TYPE_BUFFER)
  556. goto out;
  557. table = obj->buffer.pointer;
  558. switch (((struct acpi_subtable_header *)table)->type) {
  559. case ACPI_MADT_TYPE_IO_SAPIC:
  560. *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
  561. result = 0;
  562. break;
  563. case ACPI_MADT_TYPE_IO_APIC:
  564. *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
  565. result = 0;
  566. break;
  567. default:
  568. break;
  569. }
  570. out:
  571. kfree(buffer.pointer);
  572. return result;
  573. }
  574. static acpi_status
  575. ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
  576. {
  577. acpi_status status;
  578. unsigned long long sta;
  579. acpi_handle tmp;
  580. struct pci_dev *pdev;
  581. u32 gsi_base;
  582. u64 phys_addr;
  583. struct acpiphp_ioapic *ioapic;
  584. /* Evaluate _STA if present */
  585. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  586. if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
  587. return AE_CTRL_DEPTH;
  588. /* Scan only PCI bus scope */
  589. status = acpi_get_handle(handle, "_HID", &tmp);
  590. if (ACPI_SUCCESS(status))
  591. return AE_CTRL_DEPTH;
  592. if (get_gsi_base(handle, &gsi_base))
  593. return AE_OK;
  594. ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
  595. if (!ioapic)
  596. return AE_NO_MEMORY;
  597. pdev = get_apic_pci_info(handle);
  598. if (!pdev)
  599. goto exit_kfree;
  600. if (pci_enable_device(pdev))
  601. goto exit_pci_dev_put;
  602. pci_set_master(pdev);
  603. if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
  604. goto exit_pci_disable_device;
  605. phys_addr = pci_resource_start(pdev, 0);
  606. if (acpi_register_ioapic(handle, phys_addr, gsi_base))
  607. goto exit_pci_release_region;
  608. ioapic->gsi_base = gsi_base;
  609. ioapic->dev = pdev;
  610. spin_lock(&ioapic_list_lock);
  611. list_add_tail(&ioapic->list, &ioapic_list);
  612. spin_unlock(&ioapic_list_lock);
  613. return AE_OK;
  614. exit_pci_release_region:
  615. pci_release_region(pdev, 0);
  616. exit_pci_disable_device:
  617. pci_disable_device(pdev);
  618. exit_pci_dev_put:
  619. pci_dev_put(pdev);
  620. exit_kfree:
  621. kfree(ioapic);
  622. return AE_OK;
  623. }
  624. static acpi_status
  625. ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
  626. {
  627. acpi_status status;
  628. unsigned long long sta;
  629. acpi_handle tmp;
  630. u32 gsi_base;
  631. struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
  632. /* Evaluate _STA if present */
  633. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  634. if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
  635. return AE_CTRL_DEPTH;
  636. /* Scan only PCI bus scope */
  637. status = acpi_get_handle(handle, "_HID", &tmp);
  638. if (ACPI_SUCCESS(status))
  639. return AE_CTRL_DEPTH;
  640. if (get_gsi_base(handle, &gsi_base))
  641. return AE_OK;
  642. acpi_unregister_ioapic(handle, gsi_base);
  643. spin_lock(&ioapic_list_lock);
  644. list_for_each_entry_safe(pos, n, &ioapic_list, list) {
  645. if (pos->gsi_base != gsi_base)
  646. continue;
  647. ioapic = pos;
  648. list_del(&ioapic->list);
  649. break;
  650. }
  651. spin_unlock(&ioapic_list_lock);
  652. if (!ioapic)
  653. return AE_OK;
  654. pci_release_region(ioapic->dev, 0);
  655. pci_disable_device(ioapic->dev);
  656. pci_dev_put(ioapic->dev);
  657. kfree(ioapic);
  658. return AE_OK;
  659. }
  660. static int acpiphp_configure_ioapics(acpi_handle handle)
  661. {
  662. ioapic_add(handle, 0, NULL, NULL);
  663. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  664. ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
  665. return 0;
  666. }
  667. static int acpiphp_unconfigure_ioapics(acpi_handle handle)
  668. {
  669. ioapic_remove(handle, 0, NULL, NULL);
  670. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  671. ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
  672. return 0;
  673. }
  674. static int power_on_slot(struct acpiphp_slot *slot)
  675. {
  676. acpi_status status;
  677. struct acpiphp_func *func;
  678. struct list_head *l;
  679. int retval = 0;
  680. /* if already enabled, just skip */
  681. if (slot->flags & SLOT_POWEREDON)
  682. goto err_exit;
  683. list_for_each (l, &slot->funcs) {
  684. func = list_entry(l, struct acpiphp_func, sibling);
  685. if (func->flags & FUNC_HAS_PS0) {
  686. dbg("%s: executing _PS0\n", __func__);
  687. status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
  688. if (ACPI_FAILURE(status)) {
  689. warn("%s: _PS0 failed\n", __func__);
  690. retval = -1;
  691. goto err_exit;
  692. } else
  693. break;
  694. }
  695. }
  696. /* TBD: evaluate _STA to check if the slot is enabled */
  697. slot->flags |= SLOT_POWEREDON;
  698. err_exit:
  699. return retval;
  700. }
  701. static int power_off_slot(struct acpiphp_slot *slot)
  702. {
  703. acpi_status status;
  704. struct acpiphp_func *func;
  705. struct list_head *l;
  706. int retval = 0;
  707. /* if already disabled, just skip */
  708. if ((slot->flags & SLOT_POWEREDON) == 0)
  709. goto err_exit;
  710. list_for_each (l, &slot->funcs) {
  711. func = list_entry(l, struct acpiphp_func, sibling);
  712. if (func->flags & FUNC_HAS_PS3) {
  713. status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
  714. if (ACPI_FAILURE(status)) {
  715. warn("%s: _PS3 failed\n", __func__);
  716. retval = -1;
  717. goto err_exit;
  718. } else
  719. break;
  720. }
  721. }
  722. /* TBD: evaluate _STA to check if the slot is disabled */
  723. slot->flags &= (~SLOT_POWEREDON);
  724. err_exit:
  725. return retval;
  726. }
  727. /**
  728. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  729. * @bus: bus to start search with
  730. */
  731. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  732. {
  733. struct list_head *tmp;
  734. unsigned char max, n;
  735. /*
  736. * pci_bus_max_busnr will return the highest
  737. * reserved busnr for all these children.
  738. * that is equivalent to the bus->subordinate
  739. * value. We don't want to use the parent's
  740. * bus->subordinate value because it could have
  741. * padding in it.
  742. */
  743. max = bus->secondary;
  744. list_for_each(tmp, &bus->children) {
  745. n = pci_bus_max_busnr(pci_bus_b(tmp));
  746. if (n > max)
  747. max = n;
  748. }
  749. return max;
  750. }
  751. /**
  752. * acpiphp_bus_add - add a new bus to acpi subsystem
  753. * @func: acpiphp_func of the bridge
  754. */
  755. static int acpiphp_bus_add(struct acpiphp_func *func)
  756. {
  757. acpi_handle phandle;
  758. struct acpi_device *device, *pdevice;
  759. int ret_val;
  760. acpi_get_parent(func->handle, &phandle);
  761. if (acpi_bus_get_device(phandle, &pdevice)) {
  762. dbg("no parent device, assuming NULL\n");
  763. pdevice = NULL;
  764. }
  765. if (!acpi_bus_get_device(func->handle, &device)) {
  766. dbg("bus exists... trim\n");
  767. /* this shouldn't be in here, so remove
  768. * the bus then re-add it...
  769. */
  770. ret_val = acpi_bus_trim(device, 1);
  771. dbg("acpi_bus_trim return %x\n", ret_val);
  772. }
  773. ret_val = acpi_bus_add(&device, pdevice, func->handle,
  774. ACPI_BUS_TYPE_DEVICE);
  775. if (ret_val) {
  776. dbg("error adding bus, %x\n",
  777. -ret_val);
  778. goto acpiphp_bus_add_out;
  779. }
  780. /*
  781. * try to start anyway. We could have failed to add
  782. * simply because this bus had previously been added
  783. * on another add. Don't bother with the return value
  784. * we just keep going.
  785. */
  786. ret_val = acpi_bus_start(device);
  787. acpiphp_bus_add_out:
  788. return ret_val;
  789. }
  790. /**
  791. * acpiphp_bus_trim - trim a bus from acpi subsystem
  792. * @handle: handle to acpi namespace
  793. */
  794. static int acpiphp_bus_trim(acpi_handle handle)
  795. {
  796. struct acpi_device *device;
  797. int retval;
  798. retval = acpi_bus_get_device(handle, &device);
  799. if (retval) {
  800. dbg("acpi_device not found\n");
  801. return retval;
  802. }
  803. retval = acpi_bus_trim(device, 1);
  804. if (retval)
  805. err("cannot remove from acpi list\n");
  806. return retval;
  807. }
  808. /**
  809. * enable_device - enable, configure a slot
  810. * @slot: slot to be enabled
  811. *
  812. * This function should be called per *physical slot*,
  813. * not per each slot object in ACPI namespace.
  814. */
  815. static int __ref enable_device(struct acpiphp_slot *slot)
  816. {
  817. struct pci_dev *dev;
  818. struct pci_bus *bus = slot->bridge->pci_bus;
  819. struct list_head *l;
  820. struct acpiphp_func *func;
  821. int retval = 0;
  822. int num, max, pass;
  823. acpi_status status;
  824. if (slot->flags & SLOT_ENABLED)
  825. goto err_exit;
  826. /* sanity check: dev should be NULL when hot-plugged in */
  827. dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
  828. if (dev) {
  829. /* This case shouldn't happen */
  830. err("pci_dev structure already exists.\n");
  831. pci_dev_put(dev);
  832. retval = -1;
  833. goto err_exit;
  834. }
  835. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  836. if (num == 0) {
  837. err("No new device found\n");
  838. retval = -1;
  839. goto err_exit;
  840. }
  841. max = acpiphp_max_busnr(bus);
  842. for (pass = 0; pass < 2; pass++) {
  843. list_for_each_entry(dev, &bus->devices, bus_list) {
  844. if (PCI_SLOT(dev->devfn) != slot->device)
  845. continue;
  846. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  847. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  848. max = pci_scan_bridge(bus, dev, max, pass);
  849. if (pass && dev->subordinate)
  850. pci_bus_size_bridges(dev->subordinate);
  851. }
  852. }
  853. }
  854. list_for_each (l, &slot->funcs) {
  855. func = list_entry(l, struct acpiphp_func, sibling);
  856. acpiphp_bus_add(func);
  857. }
  858. pci_bus_assign_resources(bus);
  859. acpiphp_sanitize_bus(bus);
  860. acpiphp_set_hpp_values(bus);
  861. list_for_each_entry(func, &slot->funcs, sibling)
  862. acpiphp_configure_ioapics(func->handle);
  863. pci_enable_bridges(bus);
  864. pci_bus_add_devices(bus);
  865. list_for_each (l, &slot->funcs) {
  866. func = list_entry(l, struct acpiphp_func, sibling);
  867. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  868. func->function));
  869. if (!dev)
  870. continue;
  871. if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
  872. dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
  873. pci_dev_put(dev);
  874. continue;
  875. }
  876. status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
  877. if (ACPI_FAILURE(status))
  878. warn("find_p2p_bridge failed (error code = 0x%x)\n",
  879. status);
  880. pci_dev_put(dev);
  881. }
  882. slot->flags |= SLOT_ENABLED;
  883. err_exit:
  884. return retval;
  885. }
  886. static void disable_bridges(struct pci_bus *bus)
  887. {
  888. struct pci_dev *dev;
  889. list_for_each_entry(dev, &bus->devices, bus_list) {
  890. if (dev->subordinate) {
  891. disable_bridges(dev->subordinate);
  892. pci_disable_device(dev);
  893. }
  894. }
  895. }
  896. /**
  897. * disable_device - disable a slot
  898. * @slot: ACPI PHP slot
  899. */
  900. static int disable_device(struct acpiphp_slot *slot)
  901. {
  902. struct acpiphp_func *func;
  903. struct pci_dev *pdev;
  904. /* is this slot already disabled? */
  905. if (!(slot->flags & SLOT_ENABLED))
  906. goto err_exit;
  907. list_for_each_entry(func, &slot->funcs, sibling) {
  908. if (func->bridge) {
  909. /* cleanup p2p bridges under this P2P bridge */
  910. cleanup_p2p_bridge(func->bridge->handle,
  911. (u32)1, NULL, NULL);
  912. func->bridge = NULL;
  913. }
  914. pdev = pci_get_slot(slot->bridge->pci_bus,
  915. PCI_DEVFN(slot->device, func->function));
  916. if (pdev) {
  917. pci_stop_bus_device(pdev);
  918. if (pdev->subordinate) {
  919. disable_bridges(pdev->subordinate);
  920. pci_disable_device(pdev);
  921. }
  922. pci_remove_bus_device(pdev);
  923. pci_dev_put(pdev);
  924. }
  925. }
  926. list_for_each_entry(func, &slot->funcs, sibling) {
  927. acpiphp_unconfigure_ioapics(func->handle);
  928. acpiphp_bus_trim(func->handle);
  929. }
  930. slot->flags &= (~SLOT_ENABLED);
  931. err_exit:
  932. return 0;
  933. }
  934. /**
  935. * get_slot_status - get ACPI slot status
  936. * @slot: ACPI PHP slot
  937. *
  938. * If a slot has _STA for each function and if any one of them
  939. * returned non-zero status, return it.
  940. *
  941. * If a slot doesn't have _STA and if any one of its functions'
  942. * configuration space is configured, return 0x0f as a _STA.
  943. *
  944. * Otherwise return 0.
  945. */
  946. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  947. {
  948. acpi_status status;
  949. unsigned long long sta = 0;
  950. u32 dvid;
  951. struct list_head *l;
  952. struct acpiphp_func *func;
  953. list_for_each (l, &slot->funcs) {
  954. func = list_entry(l, struct acpiphp_func, sibling);
  955. if (func->flags & FUNC_HAS_STA) {
  956. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  957. if (ACPI_SUCCESS(status) && sta)
  958. break;
  959. } else {
  960. pci_bus_read_config_dword(slot->bridge->pci_bus,
  961. PCI_DEVFN(slot->device,
  962. func->function),
  963. PCI_VENDOR_ID, &dvid);
  964. if (dvid != 0xffffffff) {
  965. sta = ACPI_STA_ALL;
  966. break;
  967. }
  968. }
  969. }
  970. return (unsigned int)sta;
  971. }
  972. /**
  973. * acpiphp_eject_slot - physically eject the slot
  974. * @slot: ACPI PHP slot
  975. */
  976. int acpiphp_eject_slot(struct acpiphp_slot *slot)
  977. {
  978. acpi_status status;
  979. struct acpiphp_func *func;
  980. struct list_head *l;
  981. struct acpi_object_list arg_list;
  982. union acpi_object arg;
  983. list_for_each (l, &slot->funcs) {
  984. func = list_entry(l, struct acpiphp_func, sibling);
  985. /* We don't want to call _EJ0 on non-existing functions. */
  986. if ((func->flags & FUNC_HAS_EJ0)) {
  987. /* _EJ0 method take one argument */
  988. arg_list.count = 1;
  989. arg_list.pointer = &arg;
  990. arg.type = ACPI_TYPE_INTEGER;
  991. arg.integer.value = 1;
  992. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  993. if (ACPI_FAILURE(status)) {
  994. warn("%s: _EJ0 failed\n", __func__);
  995. return -1;
  996. } else
  997. break;
  998. }
  999. }
  1000. return 0;
  1001. }
  1002. /**
  1003. * acpiphp_check_bridge - re-enumerate devices
  1004. * @bridge: where to begin re-enumeration
  1005. *
  1006. * Iterate over all slots under this bridge and make sure that if a
  1007. * card is present they are enabled, and if not they are disabled.
  1008. */
  1009. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  1010. {
  1011. struct acpiphp_slot *slot;
  1012. int retval = 0;
  1013. int enabled, disabled;
  1014. enabled = disabled = 0;
  1015. for (slot = bridge->slots; slot; slot = slot->next) {
  1016. unsigned int status = get_slot_status(slot);
  1017. if (slot->flags & SLOT_ENABLED) {
  1018. if (status == ACPI_STA_ALL)
  1019. continue;
  1020. retval = acpiphp_disable_slot(slot);
  1021. if (retval) {
  1022. err("Error occurred in disabling\n");
  1023. goto err_exit;
  1024. } else {
  1025. acpiphp_eject_slot(slot);
  1026. }
  1027. disabled++;
  1028. } else {
  1029. if (status != ACPI_STA_ALL)
  1030. continue;
  1031. retval = acpiphp_enable_slot(slot);
  1032. if (retval) {
  1033. err("Error occurred in enabling\n");
  1034. goto err_exit;
  1035. }
  1036. enabled++;
  1037. }
  1038. }
  1039. dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
  1040. err_exit:
  1041. return retval;
  1042. }
  1043. static void program_hpp(struct pci_dev *dev, struct hotplug_params *hpp)
  1044. {
  1045. u16 pci_cmd, pci_bctl;
  1046. struct pci_dev *cdev;
  1047. /* Program hpp values for this device */
  1048. if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
  1049. (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  1050. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
  1051. return;
  1052. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
  1053. return;
  1054. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
  1055. hpp->t0->cache_line_size);
  1056. pci_write_config_byte(dev, PCI_LATENCY_TIMER,
  1057. hpp->t0->latency_timer);
  1058. pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
  1059. if (hpp->t0->enable_serr)
  1060. pci_cmd |= PCI_COMMAND_SERR;
  1061. else
  1062. pci_cmd &= ~PCI_COMMAND_SERR;
  1063. if (hpp->t0->enable_perr)
  1064. pci_cmd |= PCI_COMMAND_PARITY;
  1065. else
  1066. pci_cmd &= ~PCI_COMMAND_PARITY;
  1067. pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
  1068. /* Program bridge control value and child devices */
  1069. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  1070. pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
  1071. hpp->t0->latency_timer);
  1072. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
  1073. if (hpp->t0->enable_serr)
  1074. pci_bctl |= PCI_BRIDGE_CTL_SERR;
  1075. else
  1076. pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
  1077. if (hpp->t0->enable_perr)
  1078. pci_bctl |= PCI_BRIDGE_CTL_PARITY;
  1079. else
  1080. pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
  1081. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
  1082. if (dev->subordinate) {
  1083. list_for_each_entry(cdev, &dev->subordinate->devices,
  1084. bus_list)
  1085. program_hpp(cdev, hpp);
  1086. }
  1087. }
  1088. }
  1089. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  1090. {
  1091. struct pci_dev *dev;
  1092. struct hotplug_params hpp;
  1093. list_for_each_entry(dev, &bus->devices, bus_list) {
  1094. decode_hpp(dev, &hpp);
  1095. program_hpp(dev, &hpp);
  1096. }
  1097. }
  1098. /*
  1099. * Remove devices for which we could not assign resources, call
  1100. * arch specific code to fix-up the bus
  1101. */
  1102. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  1103. {
  1104. struct pci_dev *dev;
  1105. int i;
  1106. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  1107. list_for_each_entry(dev, &bus->devices, bus_list) {
  1108. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  1109. struct resource *res = &dev->resource[i];
  1110. if ((res->flags & type_mask) && !res->start &&
  1111. res->end) {
  1112. /* Could not assign a required resources
  1113. * for this device, remove it */
  1114. pci_remove_bus_device(dev);
  1115. break;
  1116. }
  1117. }
  1118. }
  1119. }
  1120. /* Program resources in newly inserted bridge */
  1121. static int acpiphp_configure_bridge (acpi_handle handle)
  1122. {
  1123. struct pci_bus *bus;
  1124. if (acpi_is_root_bridge(handle)) {
  1125. struct acpi_pci_root *root = acpi_pci_find_root(handle);
  1126. bus = root->bus;
  1127. } else {
  1128. struct pci_dev *pdev = acpi_get_pci_dev(handle);
  1129. bus = pdev->subordinate;
  1130. pci_dev_put(pdev);
  1131. }
  1132. pci_bus_size_bridges(bus);
  1133. pci_bus_assign_resources(bus);
  1134. acpiphp_sanitize_bus(bus);
  1135. acpiphp_set_hpp_values(bus);
  1136. pci_enable_bridges(bus);
  1137. acpiphp_configure_ioapics(handle);
  1138. return 0;
  1139. }
  1140. static void handle_bridge_insertion(acpi_handle handle, u32 type)
  1141. {
  1142. struct acpi_device *device, *pdevice;
  1143. acpi_handle phandle;
  1144. if ((type != ACPI_NOTIFY_BUS_CHECK) &&
  1145. (type != ACPI_NOTIFY_DEVICE_CHECK)) {
  1146. err("unexpected notification type %d\n", type);
  1147. return;
  1148. }
  1149. acpi_get_parent(handle, &phandle);
  1150. if (acpi_bus_get_device(phandle, &pdevice)) {
  1151. dbg("no parent device, assuming NULL\n");
  1152. pdevice = NULL;
  1153. }
  1154. if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
  1155. err("cannot add bridge to acpi list\n");
  1156. return;
  1157. }
  1158. if (!acpiphp_configure_bridge(handle) &&
  1159. !acpi_bus_start(device))
  1160. add_bridge(handle);
  1161. else
  1162. err("cannot configure and start bridge\n");
  1163. }
  1164. /*
  1165. * ACPI event handlers
  1166. */
  1167. static acpi_status
  1168. count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  1169. {
  1170. int *count = (int *)context;
  1171. struct acpiphp_bridge *bridge;
  1172. bridge = acpiphp_handle_to_bridge(handle);
  1173. if (bridge)
  1174. (*count)++;
  1175. return AE_OK ;
  1176. }
  1177. static acpi_status
  1178. check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  1179. {
  1180. struct acpiphp_bridge *bridge;
  1181. char objname[64];
  1182. struct acpi_buffer buffer = { .length = sizeof(objname),
  1183. .pointer = objname };
  1184. bridge = acpiphp_handle_to_bridge(handle);
  1185. if (bridge) {
  1186. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1187. dbg("%s: re-enumerating slots under %s\n",
  1188. __func__, objname);
  1189. acpiphp_check_bridge(bridge);
  1190. }
  1191. return AE_OK ;
  1192. }
  1193. /**
  1194. * handle_hotplug_event_bridge - handle ACPI event on bridges
  1195. * @handle: Notify()'ed acpi_handle
  1196. * @type: Notify code
  1197. * @context: pointer to acpiphp_bridge structure
  1198. *
  1199. * Handles ACPI event notification on {host,p2p} bridges.
  1200. */
  1201. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
  1202. {
  1203. struct acpiphp_bridge *bridge;
  1204. char objname[64];
  1205. struct acpi_buffer buffer = { .length = sizeof(objname),
  1206. .pointer = objname };
  1207. struct acpi_device *device;
  1208. int num_sub_bridges = 0;
  1209. if (acpi_bus_get_device(handle, &device)) {
  1210. /* This bridge must have just been physically inserted */
  1211. handle_bridge_insertion(handle, type);
  1212. return;
  1213. }
  1214. bridge = acpiphp_handle_to_bridge(handle);
  1215. if (type == ACPI_NOTIFY_BUS_CHECK) {
  1216. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
  1217. count_sub_bridges, &num_sub_bridges, NULL);
  1218. }
  1219. if (!bridge && !num_sub_bridges) {
  1220. err("cannot get bridge info\n");
  1221. return;
  1222. }
  1223. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1224. switch (type) {
  1225. case ACPI_NOTIFY_BUS_CHECK:
  1226. /* bus re-enumerate */
  1227. dbg("%s: Bus check notify on %s\n", __func__, objname);
  1228. if (bridge) {
  1229. dbg("%s: re-enumerating slots under %s\n",
  1230. __func__, objname);
  1231. acpiphp_check_bridge(bridge);
  1232. }
  1233. if (num_sub_bridges)
  1234. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  1235. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
  1236. break;
  1237. case ACPI_NOTIFY_DEVICE_CHECK:
  1238. /* device check */
  1239. dbg("%s: Device check notify on %s\n", __func__, objname);
  1240. acpiphp_check_bridge(bridge);
  1241. break;
  1242. case ACPI_NOTIFY_DEVICE_WAKE:
  1243. /* wake event */
  1244. dbg("%s: Device wake notify on %s\n", __func__, objname);
  1245. break;
  1246. case ACPI_NOTIFY_EJECT_REQUEST:
  1247. /* request device eject */
  1248. dbg("%s: Device eject notify on %s\n", __func__, objname);
  1249. if ((bridge->type != BRIDGE_TYPE_HOST) &&
  1250. (bridge->flags & BRIDGE_HAS_EJ0)) {
  1251. struct acpiphp_slot *slot;
  1252. slot = bridge->func->slot;
  1253. if (!acpiphp_disable_slot(slot))
  1254. acpiphp_eject_slot(slot);
  1255. }
  1256. break;
  1257. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  1258. printk(KERN_ERR "Device %s cannot be configured due"
  1259. " to a frequency mismatch\n", objname);
  1260. break;
  1261. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  1262. printk(KERN_ERR "Device %s cannot be configured due"
  1263. " to a bus mode mismatch\n", objname);
  1264. break;
  1265. case ACPI_NOTIFY_POWER_FAULT:
  1266. printk(KERN_ERR "Device %s has suffered a power fault\n",
  1267. objname);
  1268. break;
  1269. default:
  1270. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1271. break;
  1272. }
  1273. }
  1274. /**
  1275. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  1276. * @handle: Notify()'ed acpi_handle
  1277. * @type: Notify code
  1278. * @context: pointer to acpiphp_func structure
  1279. *
  1280. * Handles ACPI event notification on slots.
  1281. */
  1282. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
  1283. {
  1284. struct acpiphp_func *func;
  1285. char objname[64];
  1286. struct acpi_buffer buffer = { .length = sizeof(objname),
  1287. .pointer = objname };
  1288. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1289. func = (struct acpiphp_func *)context;
  1290. switch (type) {
  1291. case ACPI_NOTIFY_BUS_CHECK:
  1292. /* bus re-enumerate */
  1293. dbg("%s: Bus check notify on %s\n", __func__, objname);
  1294. acpiphp_enable_slot(func->slot);
  1295. break;
  1296. case ACPI_NOTIFY_DEVICE_CHECK:
  1297. /* device check : re-enumerate from parent bus */
  1298. dbg("%s: Device check notify on %s\n", __func__, objname);
  1299. acpiphp_check_bridge(func->slot->bridge);
  1300. break;
  1301. case ACPI_NOTIFY_DEVICE_WAKE:
  1302. /* wake event */
  1303. dbg("%s: Device wake notify on %s\n", __func__, objname);
  1304. break;
  1305. case ACPI_NOTIFY_EJECT_REQUEST:
  1306. /* request device eject */
  1307. dbg("%s: Device eject notify on %s\n", __func__, objname);
  1308. if (!(acpiphp_disable_slot(func->slot)))
  1309. acpiphp_eject_slot(func->slot);
  1310. break;
  1311. default:
  1312. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1313. break;
  1314. }
  1315. }
  1316. static acpi_status
  1317. find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  1318. {
  1319. int *count = (int *)context;
  1320. if (acpi_is_root_bridge(handle)) {
  1321. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  1322. handle_hotplug_event_bridge, NULL);
  1323. (*count)++;
  1324. }
  1325. return AE_OK ;
  1326. }
  1327. static struct acpi_pci_driver acpi_pci_hp_driver = {
  1328. .add = add_bridge,
  1329. .remove = remove_bridge,
  1330. };
  1331. /**
  1332. * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
  1333. */
  1334. int __init acpiphp_glue_init(void)
  1335. {
  1336. int num = 0;
  1337. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  1338. ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
  1339. if (num <= 0)
  1340. return -1;
  1341. else
  1342. acpi_pci_register_driver(&acpi_pci_hp_driver);
  1343. return 0;
  1344. }
  1345. /**
  1346. * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
  1347. *
  1348. * This function frees all data allocated in acpiphp_glue_init().
  1349. */
  1350. void acpiphp_glue_exit(void)
  1351. {
  1352. acpi_pci_unregister_driver(&acpi_pci_hp_driver);
  1353. }
  1354. /**
  1355. * acpiphp_get_num_slots - count number of slots in a system
  1356. */
  1357. int __init acpiphp_get_num_slots(void)
  1358. {
  1359. struct acpiphp_bridge *bridge;
  1360. int num_slots = 0;
  1361. list_for_each_entry (bridge, &bridge_list, list) {
  1362. dbg("Bus %04x:%02x has %d slot%s\n",
  1363. pci_domain_nr(bridge->pci_bus),
  1364. bridge->pci_bus->number, bridge->nr_slots,
  1365. bridge->nr_slots == 1 ? "" : "s");
  1366. num_slots += bridge->nr_slots;
  1367. }
  1368. dbg("Total %d slots\n", num_slots);
  1369. return num_slots;
  1370. }
  1371. #if 0
  1372. /**
  1373. * acpiphp_for_each_slot - call function for each slot
  1374. * @fn: callback function
  1375. * @data: context to be passed to callback function
  1376. */
  1377. static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
  1378. {
  1379. struct list_head *node;
  1380. struct acpiphp_bridge *bridge;
  1381. struct acpiphp_slot *slot;
  1382. int retval = 0;
  1383. list_for_each (node, &bridge_list) {
  1384. bridge = (struct acpiphp_bridge *)node;
  1385. for (slot = bridge->slots; slot; slot = slot->next) {
  1386. retval = fn(slot, data);
  1387. if (!retval)
  1388. goto err_exit;
  1389. }
  1390. }
  1391. err_exit:
  1392. return retval;
  1393. }
  1394. #endif
  1395. /**
  1396. * acpiphp_enable_slot - power on slot
  1397. * @slot: ACPI PHP slot
  1398. */
  1399. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1400. {
  1401. int retval;
  1402. mutex_lock(&slot->crit_sect);
  1403. /* wake up all functions */
  1404. retval = power_on_slot(slot);
  1405. if (retval)
  1406. goto err_exit;
  1407. if (get_slot_status(slot) == ACPI_STA_ALL) {
  1408. /* configure all functions */
  1409. retval = enable_device(slot);
  1410. if (retval)
  1411. power_off_slot(slot);
  1412. } else {
  1413. dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
  1414. power_off_slot(slot);
  1415. }
  1416. err_exit:
  1417. mutex_unlock(&slot->crit_sect);
  1418. return retval;
  1419. }
  1420. /**
  1421. * acpiphp_disable_slot - power off slot
  1422. * @slot: ACPI PHP slot
  1423. */
  1424. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1425. {
  1426. int retval = 0;
  1427. mutex_lock(&slot->crit_sect);
  1428. /* unconfigure all functions */
  1429. retval = disable_device(slot);
  1430. if (retval)
  1431. goto err_exit;
  1432. /* power off all functions */
  1433. retval = power_off_slot(slot);
  1434. if (retval)
  1435. goto err_exit;
  1436. err_exit:
  1437. mutex_unlock(&slot->crit_sect);
  1438. return retval;
  1439. }
  1440. /*
  1441. * slot enabled: 1
  1442. * slot disabled: 0
  1443. */
  1444. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1445. {
  1446. return (slot->flags & SLOT_POWEREDON);
  1447. }
  1448. /*
  1449. * latch open: 1
  1450. * latch closed: 0
  1451. */
  1452. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1453. {
  1454. unsigned int sta;
  1455. sta = get_slot_status(slot);
  1456. return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
  1457. }
  1458. /*
  1459. * adapter presence : 1
  1460. * absence : 0
  1461. */
  1462. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1463. {
  1464. unsigned int sta;
  1465. sta = get_slot_status(slot);
  1466. return (sta == 0) ? 0 : 1;
  1467. }