acpiphp_glue.c 39 KB

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