acpiphp_glue.c 42 KB

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