acpiphp_glue.c 45 KB

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