acpiphp_glue.c 39 KB

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