acpiphp_glue.c 45 KB

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