acpiphp_glue.c 34 KB

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