acpiphp_glue.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  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. if (pass && dev->subordinate)
  645. pci_bus_size_bridges(dev->subordinate);
  646. }
  647. }
  648. }
  649. pci_bus_assign_resources(bus);
  650. acpiphp_sanitize_bus(bus);
  651. pci_enable_bridges(bus);
  652. pci_bus_add_devices(bus);
  653. acpiphp_set_hpp_values(DEVICE_ACPI_HANDLE(&bus->self->dev), bus);
  654. acpiphp_configure_ioapics(DEVICE_ACPI_HANDLE(&bus->self->dev));
  655. /* associate pci_dev to our representation */
  656. list_for_each (l, &slot->funcs) {
  657. func = list_entry(l, struct acpiphp_func, sibling);
  658. func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  659. func->function));
  660. }
  661. slot->flags |= SLOT_ENABLED;
  662. err_exit:
  663. return retval;
  664. }
  665. /**
  666. * disable_device - disable a slot
  667. */
  668. static int disable_device(struct acpiphp_slot *slot)
  669. {
  670. int retval = 0;
  671. struct acpiphp_func *func;
  672. struct list_head *l;
  673. /* is this slot already disabled? */
  674. if (!(slot->flags & SLOT_ENABLED))
  675. goto err_exit;
  676. list_for_each (l, &slot->funcs) {
  677. func = list_entry(l, struct acpiphp_func, sibling);
  678. if (!func->pci_dev)
  679. continue;
  680. pci_remove_bus_device(func->pci_dev);
  681. pci_dev_put(func->pci_dev);
  682. func->pci_dev = NULL;
  683. }
  684. slot->flags &= (~SLOT_ENABLED);
  685. err_exit:
  686. return retval;
  687. }
  688. /**
  689. * get_slot_status - get ACPI slot status
  690. *
  691. * if a slot has _STA for each function and if any one of them
  692. * returned non-zero status, return it
  693. *
  694. * if a slot doesn't have _STA and if any one of its functions'
  695. * configuration space is configured, return 0x0f as a _STA
  696. *
  697. * otherwise return 0
  698. */
  699. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  700. {
  701. acpi_status status;
  702. unsigned long sta = 0;
  703. u32 dvid;
  704. struct list_head *l;
  705. struct acpiphp_func *func;
  706. list_for_each (l, &slot->funcs) {
  707. func = list_entry(l, struct acpiphp_func, sibling);
  708. if (func->flags & FUNC_HAS_STA) {
  709. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  710. if (ACPI_SUCCESS(status) && sta)
  711. break;
  712. } else {
  713. pci_bus_read_config_dword(slot->bridge->pci_bus,
  714. PCI_DEVFN(slot->device,
  715. func->function),
  716. PCI_VENDOR_ID, &dvid);
  717. if (dvid != 0xffffffff) {
  718. sta = ACPI_STA_ALL;
  719. break;
  720. }
  721. }
  722. }
  723. return (unsigned int)sta;
  724. }
  725. /**
  726. * acpiphp_eject_slot - physically eject the slot
  727. */
  728. static int acpiphp_eject_slot(struct acpiphp_slot *slot)
  729. {
  730. acpi_status status;
  731. struct acpiphp_func *func;
  732. struct list_head *l;
  733. struct acpi_object_list arg_list;
  734. union acpi_object arg;
  735. list_for_each (l, &slot->funcs) {
  736. func = list_entry(l, struct acpiphp_func, sibling);
  737. /* We don't want to call _EJ0 on non-existing functions. */
  738. if ((func->flags & FUNC_HAS_EJ0)) {
  739. /* _EJ0 method take one argument */
  740. arg_list.count = 1;
  741. arg_list.pointer = &arg;
  742. arg.type = ACPI_TYPE_INTEGER;
  743. arg.integer.value = 1;
  744. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  745. if (ACPI_FAILURE(status)) {
  746. warn("%s: _EJ0 failed\n", __FUNCTION__);
  747. return -1;
  748. } else
  749. break;
  750. }
  751. }
  752. return 0;
  753. }
  754. /**
  755. * acpiphp_check_bridge - re-enumerate devices
  756. *
  757. * Iterate over all slots under this bridge and make sure that if a
  758. * card is present they are enabled, and if not they are disabled.
  759. */
  760. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  761. {
  762. struct acpiphp_slot *slot;
  763. int retval = 0;
  764. int enabled, disabled;
  765. enabled = disabled = 0;
  766. for (slot = bridge->slots; slot; slot = slot->next) {
  767. unsigned int status = get_slot_status(slot);
  768. if (slot->flags & SLOT_ENABLED) {
  769. if (status == ACPI_STA_ALL)
  770. continue;
  771. retval = acpiphp_disable_slot(slot);
  772. if (retval) {
  773. err("Error occurred in disabling\n");
  774. goto err_exit;
  775. } else {
  776. acpiphp_eject_slot(slot);
  777. }
  778. disabled++;
  779. } else {
  780. if (status != ACPI_STA_ALL)
  781. continue;
  782. retval = acpiphp_enable_slot(slot);
  783. if (retval) {
  784. err("Error occurred in enabling\n");
  785. goto err_exit;
  786. }
  787. enabled++;
  788. }
  789. }
  790. dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
  791. err_exit:
  792. return retval;
  793. }
  794. static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
  795. {
  796. u16 pci_cmd, pci_bctl;
  797. struct pci_dev *cdev;
  798. /* Program hpp values for this device */
  799. if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
  800. (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  801. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
  802. return;
  803. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
  804. bridge->hpp.cache_line_size);
  805. pci_write_config_byte(dev, PCI_LATENCY_TIMER,
  806. bridge->hpp.latency_timer);
  807. pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
  808. if (bridge->hpp.enable_SERR)
  809. pci_cmd |= PCI_COMMAND_SERR;
  810. else
  811. pci_cmd &= ~PCI_COMMAND_SERR;
  812. if (bridge->hpp.enable_PERR)
  813. pci_cmd |= PCI_COMMAND_PARITY;
  814. else
  815. pci_cmd &= ~PCI_COMMAND_PARITY;
  816. pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
  817. /* Program bridge control value and child devices */
  818. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  819. pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
  820. bridge->hpp.latency_timer);
  821. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
  822. if (bridge->hpp.enable_SERR)
  823. pci_bctl |= PCI_BRIDGE_CTL_SERR;
  824. else
  825. pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
  826. if (bridge->hpp.enable_PERR)
  827. pci_bctl |= PCI_BRIDGE_CTL_PARITY;
  828. else
  829. pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
  830. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
  831. if (dev->subordinate) {
  832. list_for_each_entry(cdev, &dev->subordinate->devices,
  833. bus_list)
  834. program_hpp(cdev, bridge);
  835. }
  836. }
  837. }
  838. static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
  839. {
  840. struct acpiphp_bridge bridge;
  841. struct pci_dev *dev;
  842. memset(&bridge, 0, sizeof(bridge));
  843. bridge.handle = handle;
  844. decode_hpp(&bridge);
  845. list_for_each_entry(dev, &bus->devices, bus_list)
  846. program_hpp(dev, &bridge);
  847. }
  848. /*
  849. * Remove devices for which we could not assign resources, call
  850. * arch specific code to fix-up the bus
  851. */
  852. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  853. {
  854. struct pci_dev *dev;
  855. int i;
  856. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  857. list_for_each_entry(dev, &bus->devices, bus_list) {
  858. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  859. struct resource *res = &dev->resource[i];
  860. if ((res->flags & type_mask) && !res->start &&
  861. res->end) {
  862. /* Could not assign a required resources
  863. * for this device, remove it */
  864. pci_remove_bus_device(dev);
  865. break;
  866. }
  867. }
  868. }
  869. }
  870. /* Program resources in newly inserted bridge */
  871. static int acpiphp_configure_bridge (acpi_handle handle)
  872. {
  873. struct acpi_pci_id pci_id;
  874. struct pci_bus *bus;
  875. if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
  876. err("cannot get PCI domain and bus number for bridge\n");
  877. return -EINVAL;
  878. }
  879. bus = pci_find_bus(pci_id.segment, pci_id.bus);
  880. if (!bus) {
  881. err("cannot find bus %d:%d\n",
  882. pci_id.segment, pci_id.bus);
  883. return -EINVAL;
  884. }
  885. pci_bus_size_bridges(bus);
  886. pci_bus_assign_resources(bus);
  887. acpiphp_sanitize_bus(bus);
  888. acpiphp_set_hpp_values(handle, bus);
  889. pci_enable_bridges(bus);
  890. acpiphp_configure_ioapics(handle);
  891. return 0;
  892. }
  893. static void handle_bridge_insertion(acpi_handle handle, u32 type)
  894. {
  895. struct acpi_device *device, *pdevice;
  896. acpi_handle phandle;
  897. if ((type != ACPI_NOTIFY_BUS_CHECK) &&
  898. (type != ACPI_NOTIFY_DEVICE_CHECK)) {
  899. err("unexpected notification type %d\n", type);
  900. return;
  901. }
  902. acpi_get_parent(handle, &phandle);
  903. if (acpi_bus_get_device(phandle, &pdevice)) {
  904. dbg("no parent device, assuming NULL\n");
  905. pdevice = NULL;
  906. }
  907. if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
  908. err("cannot add bridge to acpi list\n");
  909. return;
  910. }
  911. if (!acpiphp_configure_bridge(handle) &&
  912. !acpi_bus_start(device))
  913. add_bridge(handle);
  914. else
  915. err("cannot configure and start bridge\n");
  916. }
  917. /*
  918. * ACPI event handlers
  919. */
  920. /**
  921. * handle_hotplug_event_bridge - handle ACPI event on bridges
  922. *
  923. * @handle: Notify()'ed acpi_handle
  924. * @type: Notify code
  925. * @context: pointer to acpiphp_bridge structure
  926. *
  927. * handles ACPI event notification on {host,p2p} bridges
  928. *
  929. */
  930. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
  931. {
  932. struct acpiphp_bridge *bridge;
  933. char objname[64];
  934. struct acpi_buffer buffer = { .length = sizeof(objname),
  935. .pointer = objname };
  936. struct acpi_device *device;
  937. if (acpi_bus_get_device(handle, &device)) {
  938. /* This bridge must have just been physically inserted */
  939. handle_bridge_insertion(handle, type);
  940. return;
  941. }
  942. bridge = acpiphp_handle_to_bridge(handle);
  943. if (!bridge) {
  944. err("cannot get bridge info\n");
  945. return;
  946. }
  947. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  948. switch (type) {
  949. case ACPI_NOTIFY_BUS_CHECK:
  950. /* bus re-enumerate */
  951. dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
  952. acpiphp_check_bridge(bridge);
  953. break;
  954. case ACPI_NOTIFY_DEVICE_CHECK:
  955. /* device check */
  956. dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
  957. acpiphp_check_bridge(bridge);
  958. break;
  959. case ACPI_NOTIFY_DEVICE_WAKE:
  960. /* wake event */
  961. dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
  962. break;
  963. case ACPI_NOTIFY_EJECT_REQUEST:
  964. /* request device eject */
  965. dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
  966. break;
  967. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  968. printk(KERN_ERR "Device %s cannot be configured due"
  969. " to a frequency mismatch\n", objname);
  970. break;
  971. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  972. printk(KERN_ERR "Device %s cannot be configured due"
  973. " to a bus mode mismatch\n", objname);
  974. break;
  975. case ACPI_NOTIFY_POWER_FAULT:
  976. printk(KERN_ERR "Device %s has suffered a power fault\n",
  977. objname);
  978. break;
  979. default:
  980. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  981. break;
  982. }
  983. }
  984. /**
  985. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  986. *
  987. * @handle: Notify()'ed acpi_handle
  988. * @type: Notify code
  989. * @context: pointer to acpiphp_func structure
  990. *
  991. * handles ACPI event notification on slots
  992. *
  993. */
  994. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
  995. {
  996. struct acpiphp_func *func;
  997. char objname[64];
  998. struct acpi_buffer buffer = { .length = sizeof(objname),
  999. .pointer = objname };
  1000. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1001. func = (struct acpiphp_func *)context;
  1002. switch (type) {
  1003. case ACPI_NOTIFY_BUS_CHECK:
  1004. /* bus re-enumerate */
  1005. dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
  1006. acpiphp_enable_slot(func->slot);
  1007. break;
  1008. case ACPI_NOTIFY_DEVICE_CHECK:
  1009. /* device check : re-enumerate from parent bus */
  1010. dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
  1011. acpiphp_check_bridge(func->slot->bridge);
  1012. break;
  1013. case ACPI_NOTIFY_DEVICE_WAKE:
  1014. /* wake event */
  1015. dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
  1016. break;
  1017. case ACPI_NOTIFY_EJECT_REQUEST:
  1018. /* request device eject */
  1019. dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
  1020. if (!(acpiphp_disable_slot(func->slot)))
  1021. acpiphp_eject_slot(func->slot);
  1022. break;
  1023. default:
  1024. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1025. break;
  1026. }
  1027. }
  1028. static int is_root_bridge(acpi_handle handle)
  1029. {
  1030. acpi_status status;
  1031. struct acpi_device_info *info;
  1032. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  1033. int i;
  1034. status = acpi_get_object_info(handle, &buffer);
  1035. if (ACPI_SUCCESS(status)) {
  1036. info = buffer.pointer;
  1037. if ((info->valid & ACPI_VALID_HID) &&
  1038. !strcmp(PCI_ROOT_HID_STRING,
  1039. info->hardware_id.value)) {
  1040. acpi_os_free(buffer.pointer);
  1041. return 1;
  1042. }
  1043. if (info->valid & ACPI_VALID_CID) {
  1044. for (i=0; i < info->compatibility_id.count; i++) {
  1045. if (!strcmp(PCI_ROOT_HID_STRING,
  1046. info->compatibility_id.id[i].value)) {
  1047. acpi_os_free(buffer.pointer);
  1048. return 1;
  1049. }
  1050. }
  1051. }
  1052. }
  1053. return 0;
  1054. }
  1055. static acpi_status
  1056. find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  1057. {
  1058. int *count = (int *)context;
  1059. if (is_root_bridge(handle)) {
  1060. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  1061. handle_hotplug_event_bridge, NULL);
  1062. (*count)++;
  1063. }
  1064. return AE_OK ;
  1065. }
  1066. static struct acpi_pci_driver acpi_pci_hp_driver = {
  1067. .add = add_bridge,
  1068. .remove = remove_bridge,
  1069. };
  1070. /**
  1071. * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
  1072. *
  1073. */
  1074. int __init acpiphp_glue_init(void)
  1075. {
  1076. int num = 0;
  1077. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  1078. ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
  1079. if (num <= 0)
  1080. return -1;
  1081. else
  1082. acpi_pci_register_driver(&acpi_pci_hp_driver);
  1083. return 0;
  1084. }
  1085. /**
  1086. * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
  1087. *
  1088. * This function frees all data allocated in acpiphp_glue_init()
  1089. */
  1090. void __exit acpiphp_glue_exit(void)
  1091. {
  1092. acpi_pci_unregister_driver(&acpi_pci_hp_driver);
  1093. }
  1094. /**
  1095. * acpiphp_get_num_slots - count number of slots in a system
  1096. */
  1097. int __init acpiphp_get_num_slots(void)
  1098. {
  1099. struct list_head *node;
  1100. struct acpiphp_bridge *bridge;
  1101. int num_slots;
  1102. num_slots = 0;
  1103. list_for_each (node, &bridge_list) {
  1104. bridge = (struct acpiphp_bridge *)node;
  1105. dbg("Bus %04x:%02x has %d slot%s\n",
  1106. pci_domain_nr(bridge->pci_bus),
  1107. bridge->pci_bus->number, bridge->nr_slots,
  1108. bridge->nr_slots == 1 ? "" : "s");
  1109. num_slots += bridge->nr_slots;
  1110. }
  1111. dbg("Total %d slots\n", num_slots);
  1112. return num_slots;
  1113. }
  1114. #if 0
  1115. /**
  1116. * acpiphp_for_each_slot - call function for each slot
  1117. * @fn: callback function
  1118. * @data: context to be passed to callback function
  1119. *
  1120. */
  1121. static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
  1122. {
  1123. struct list_head *node;
  1124. struct acpiphp_bridge *bridge;
  1125. struct acpiphp_slot *slot;
  1126. int retval = 0;
  1127. list_for_each (node, &bridge_list) {
  1128. bridge = (struct acpiphp_bridge *)node;
  1129. for (slot = bridge->slots; slot; slot = slot->next) {
  1130. retval = fn(slot, data);
  1131. if (!retval)
  1132. goto err_exit;
  1133. }
  1134. }
  1135. err_exit:
  1136. return retval;
  1137. }
  1138. #endif
  1139. /* search matching slot from id */
  1140. struct acpiphp_slot *get_slot_from_id(int id)
  1141. {
  1142. struct list_head *node;
  1143. struct acpiphp_bridge *bridge;
  1144. struct acpiphp_slot *slot;
  1145. list_for_each (node, &bridge_list) {
  1146. bridge = (struct acpiphp_bridge *)node;
  1147. for (slot = bridge->slots; slot; slot = slot->next)
  1148. if (slot->id == id)
  1149. return slot;
  1150. }
  1151. /* should never happen! */
  1152. err("%s: no object for id %d\n", __FUNCTION__, id);
  1153. WARN_ON(1);
  1154. return NULL;
  1155. }
  1156. /**
  1157. * acpiphp_enable_slot - power on slot
  1158. */
  1159. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1160. {
  1161. int retval;
  1162. down(&slot->crit_sect);
  1163. /* wake up all functions */
  1164. retval = power_on_slot(slot);
  1165. if (retval)
  1166. goto err_exit;
  1167. if (get_slot_status(slot) == ACPI_STA_ALL)
  1168. /* configure all functions */
  1169. retval = enable_device(slot);
  1170. err_exit:
  1171. up(&slot->crit_sect);
  1172. return retval;
  1173. }
  1174. /**
  1175. * acpiphp_disable_slot - power off slot
  1176. */
  1177. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1178. {
  1179. int retval = 0;
  1180. down(&slot->crit_sect);
  1181. /* unconfigure all functions */
  1182. retval = disable_device(slot);
  1183. if (retval)
  1184. goto err_exit;
  1185. /* power off all functions */
  1186. retval = power_off_slot(slot);
  1187. if (retval)
  1188. goto err_exit;
  1189. err_exit:
  1190. up(&slot->crit_sect);
  1191. return retval;
  1192. }
  1193. /*
  1194. * slot enabled: 1
  1195. * slot disabled: 0
  1196. */
  1197. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1198. {
  1199. return (slot->flags & SLOT_POWEREDON);
  1200. }
  1201. /*
  1202. * latch closed: 1
  1203. * latch open: 0
  1204. */
  1205. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1206. {
  1207. unsigned int sta;
  1208. sta = get_slot_status(slot);
  1209. return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
  1210. }
  1211. /*
  1212. * adapter presence : 1
  1213. * absence : 0
  1214. */
  1215. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1216. {
  1217. unsigned int sta;
  1218. sta = get_slot_status(slot);
  1219. return (sta == 0) ? 0 : 1;
  1220. }
  1221. /*
  1222. * pci address (seg/bus/dev)
  1223. */
  1224. u32 acpiphp_get_address(struct acpiphp_slot *slot)
  1225. {
  1226. u32 address;
  1227. struct pci_bus *pci_bus = slot->bridge->pci_bus;
  1228. address = (pci_domain_nr(pci_bus) << 16) |
  1229. (pci_bus->number << 8) |
  1230. slot->device;
  1231. return address;
  1232. }