shpchprm_nonacpi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * SHPCHPRM NONACPI: PHP Resource Manager for Non-ACPI/Legacy platform
  3. *
  4. * Copyright (C) 1995,2001 Compaq Computer Corporation
  5. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2001 IBM Corp.
  7. * Copyright (C) 2003-2004 Intel Corporation
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  19. * NON INFRINGEMENT. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. * Send feedback to <greg@kroah.com>, <dely.l.sy@intel.com>
  27. *
  28. */
  29. #include <linux/config.h>
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/types.h>
  33. #include <linux/pci.h>
  34. #include <linux/init.h>
  35. #include <asm/uaccess.h>
  36. #ifdef CONFIG_IA64
  37. #include <asm/iosapic.h>
  38. #endif
  39. #include "shpchp.h"
  40. #include "shpchprm.h"
  41. #include "shpchprm_nonacpi.h"
  42. void shpchprm_cleanup(void)
  43. {
  44. return;
  45. }
  46. int shpchprm_print_pirt(void)
  47. {
  48. return 0;
  49. }
  50. int shpchprm_get_physical_slot_number(struct controller *ctrl, u32 *sun, u8 busnum, u8 devnum)
  51. {
  52. int offset = devnum - ctrl->slot_device_offset;
  53. dbg("%s: ctrl->slot_num_inc %d, offset %d\n", __FUNCTION__, ctrl->slot_num_inc, offset);
  54. *sun = (u8) (ctrl->first_slot + ctrl->slot_num_inc * offset);
  55. return 0;
  56. }
  57. static void print_pci_resource ( struct pci_resource *aprh)
  58. {
  59. struct pci_resource *res;
  60. for (res = aprh; res; res = res->next)
  61. dbg(" base= 0x%x length= 0x%x\n", res->base, res->length);
  62. }
  63. static void phprm_dump_func_res( struct pci_func *fun)
  64. {
  65. struct pci_func *func = fun;
  66. if (func->bus_head) {
  67. dbg(": BUS Resources:\n");
  68. print_pci_resource (func->bus_head);
  69. }
  70. if (func->io_head) {
  71. dbg(": IO Resources:\n");
  72. print_pci_resource (func->io_head);
  73. }
  74. if (func->mem_head) {
  75. dbg(": MEM Resources:\n");
  76. print_pci_resource (func->mem_head);
  77. }
  78. if (func->p_mem_head) {
  79. dbg(": PMEM Resources:\n");
  80. print_pci_resource (func->p_mem_head);
  81. }
  82. }
  83. static int phprm_get_used_resources (
  84. struct controller *ctrl,
  85. struct pci_func *func
  86. )
  87. {
  88. return shpchp_save_used_resources (ctrl, func, !DISABLE_CARD);
  89. }
  90. static int phprm_delete_resource(
  91. struct pci_resource **aprh,
  92. ulong base,
  93. ulong size)
  94. {
  95. struct pci_resource *res;
  96. struct pci_resource *prevnode;
  97. struct pci_resource *split_node;
  98. ulong tbase;
  99. shpchp_resource_sort_and_combine(aprh);
  100. for (res = *aprh; res; res = res->next) {
  101. if (res->base > base)
  102. continue;
  103. if ((res->base + res->length) < (base + size))
  104. continue;
  105. if (res->base < base) {
  106. tbase = base;
  107. if ((res->length - (tbase - res->base)) < size)
  108. continue;
  109. split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  110. if (!split_node)
  111. return -ENOMEM;
  112. split_node->base = res->base;
  113. split_node->length = tbase - res->base;
  114. res->base = tbase;
  115. res->length -= split_node->length;
  116. split_node->next = res->next;
  117. res->next = split_node;
  118. }
  119. if (res->length >= size) {
  120. split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  121. if (!split_node)
  122. return -ENOMEM;
  123. split_node->base = res->base + size;
  124. split_node->length = res->length - size;
  125. res->length = size;
  126. split_node->next = res->next;
  127. res->next = split_node;
  128. }
  129. if (*aprh == res) {
  130. *aprh = res->next;
  131. } else {
  132. prevnode = *aprh;
  133. while (prevnode->next != res)
  134. prevnode = prevnode->next;
  135. prevnode->next = res->next;
  136. }
  137. res->next = NULL;
  138. kfree(res);
  139. break;
  140. }
  141. return 0;
  142. }
  143. static int phprm_delete_resources(
  144. struct pci_resource **aprh,
  145. struct pci_resource *this
  146. )
  147. {
  148. struct pci_resource *res;
  149. for (res = this; res; res = res->next)
  150. phprm_delete_resource(aprh, res->base, res->length);
  151. return 0;
  152. }
  153. static int configure_existing_function(
  154. struct controller *ctrl,
  155. struct pci_func *func
  156. )
  157. {
  158. int rc;
  159. /* see how much resources the func has used. */
  160. rc = phprm_get_used_resources (ctrl, func);
  161. if (!rc) {
  162. /* subtract the resources used by the func from ctrl resources */
  163. rc = phprm_delete_resources (&ctrl->bus_head, func->bus_head);
  164. rc |= phprm_delete_resources (&ctrl->io_head, func->io_head);
  165. rc |= phprm_delete_resources (&ctrl->mem_head, func->mem_head);
  166. rc |= phprm_delete_resources (&ctrl->p_mem_head, func->p_mem_head);
  167. if (rc)
  168. warn("aCEF: cannot del used resources\n");
  169. } else
  170. err("aCEF: cannot get used resources\n");
  171. return rc;
  172. }
  173. static int bind_pci_resources_to_slots ( struct controller *ctrl)
  174. {
  175. struct pci_func *func, new_func;
  176. int busn = ctrl->slot_bus;
  177. int devn, funn;
  178. u32 vid;
  179. for (devn = 0; devn < 32; devn++) {
  180. for (funn = 0; funn < 8; funn++) {
  181. /*
  182. if (devn == ctrl->device && funn == ctrl->function)
  183. continue;
  184. */
  185. /* find out if this entry is for an occupied slot */
  186. vid = 0xFFFFFFFF;
  187. pci_bus_read_config_dword(ctrl->pci_dev->subordinate, PCI_DEVFN(devn, funn), PCI_VENDOR_ID, &vid);
  188. if (vid != 0xFFFFFFFF) {
  189. func = shpchp_slot_find(busn, devn, funn);
  190. if (!func) {
  191. memset(&new_func, 0, sizeof(struct pci_func));
  192. new_func.bus = busn;
  193. new_func.device = devn;
  194. new_func.function = funn;
  195. new_func.is_a_board = 1;
  196. configure_existing_function(ctrl, &new_func);
  197. phprm_dump_func_res(&new_func);
  198. } else {
  199. configure_existing_function(ctrl, func);
  200. phprm_dump_func_res(func);
  201. }
  202. dbg("aCCF:existing PCI 0x%x Func ResourceDump\n", ctrl->bus);
  203. }
  204. }
  205. }
  206. return 0;
  207. }
  208. static void phprm_dump_ctrl_res( struct controller *ctlr)
  209. {
  210. struct controller *ctrl = ctlr;
  211. if (ctrl->bus_head) {
  212. dbg(": BUS Resources:\n");
  213. print_pci_resource (ctrl->bus_head);
  214. }
  215. if (ctrl->io_head) {
  216. dbg(": IO Resources:\n");
  217. print_pci_resource (ctrl->io_head);
  218. }
  219. if (ctrl->mem_head) {
  220. dbg(": MEM Resources:\n");
  221. print_pci_resource (ctrl->mem_head);
  222. }
  223. if (ctrl->p_mem_head) {
  224. dbg(": PMEM Resources:\n");
  225. print_pci_resource (ctrl->p_mem_head);
  226. }
  227. }
  228. /*
  229. * phprm_find_available_resources
  230. *
  231. * Finds available memory, IO, and IRQ resources for programming
  232. * devices which may be added to the system
  233. * this function is for hot plug ADD!
  234. *
  235. * returns 0 if success
  236. */
  237. int shpchprm_find_available_resources(struct controller *ctrl)
  238. {
  239. struct pci_func func;
  240. u32 rc;
  241. memset(&func, 0, sizeof(struct pci_func));
  242. func.bus = ctrl->bus;
  243. func.device = ctrl->device;
  244. func.function = ctrl->function;
  245. func.is_a_board = 1;
  246. /* Get resources for this PCI bridge */
  247. rc = shpchp_save_used_resources (ctrl, &func, !DISABLE_CARD);
  248. dbg("%s: shpchp_save_used_resources rc = %d\n", __FUNCTION__, rc);
  249. if (func.mem_head)
  250. func.mem_head->next = ctrl->mem_head;
  251. ctrl->mem_head = func.mem_head;
  252. if (func.p_mem_head)
  253. func.p_mem_head->next = ctrl->p_mem_head;
  254. ctrl->p_mem_head = func.p_mem_head;
  255. if (func.io_head)
  256. func.io_head->next = ctrl->io_head;
  257. ctrl->io_head = func.io_head;
  258. if(func.bus_head)
  259. func.bus_head->next = ctrl->bus_head;
  260. ctrl->bus_head = func.bus_head;
  261. if (ctrl->bus_head)
  262. phprm_delete_resource(&ctrl->bus_head, ctrl->pci_dev->subordinate->number, 1);
  263. dbg("%s:pre-Bind PCI 0x%x Ctrl Resource Dump\n", __FUNCTION__, ctrl->bus);
  264. phprm_dump_ctrl_res(ctrl);
  265. bind_pci_resources_to_slots (ctrl);
  266. dbg("%s:post-Bind PCI 0x%x Ctrl Resource Dump\n", __FUNCTION__, ctrl->bus);
  267. phprm_dump_ctrl_res(ctrl);
  268. /* If all of the following fail, we don't have any resources for hot plug add */
  269. rc = 1;
  270. rc &= shpchp_resource_sort_and_combine(&(ctrl->mem_head));
  271. rc &= shpchp_resource_sort_and_combine(&(ctrl->p_mem_head));
  272. rc &= shpchp_resource_sort_and_combine(&(ctrl->io_head));
  273. rc &= shpchp_resource_sort_and_combine(&(ctrl->bus_head));
  274. return (rc);
  275. }
  276. int shpchprm_set_hpp(
  277. struct controller *ctrl,
  278. struct pci_func *func,
  279. u8 card_type)
  280. {
  281. u32 rc;
  282. u8 temp_byte;
  283. struct pci_bus lpci_bus, *pci_bus;
  284. unsigned int devfn;
  285. memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
  286. pci_bus = &lpci_bus;
  287. pci_bus->number = func->bus;
  288. devfn = PCI_DEVFN(func->device, func->function);
  289. temp_byte = 0x40; /* hard coded value for LT */
  290. if (card_type == PCI_HEADER_TYPE_BRIDGE) {
  291. /* set subordinate Latency Timer */
  292. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
  293. if (rc) {
  294. dbg("%s: set secondary LT error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, func->bus,
  295. func->device, func->function);
  296. return rc;
  297. }
  298. }
  299. /* set base Latency Timer */
  300. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
  301. if (rc) {
  302. dbg("%s: set LT error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, func->bus, func->device, func->function);
  303. return rc;
  304. }
  305. /* set Cache Line size */
  306. temp_byte = 0x08; /* hard coded value for CLS */
  307. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
  308. if (rc) {
  309. dbg("%s: set CLS error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, func->bus, func->device, func->function);
  310. }
  311. /* set enable_perr */
  312. /* set enable_serr */
  313. return rc;
  314. }
  315. void shpchprm_enable_card(
  316. struct controller *ctrl,
  317. struct pci_func *func,
  318. u8 card_type)
  319. {
  320. u16 command, bcommand;
  321. struct pci_bus lpci_bus, *pci_bus;
  322. unsigned int devfn;
  323. int rc;
  324. memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
  325. pci_bus = &lpci_bus;
  326. pci_bus->number = func->bus;
  327. devfn = PCI_DEVFN(func->device, func->function);
  328. rc = pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &command);
  329. command |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR
  330. | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
  331. | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
  332. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
  333. if (card_type == PCI_HEADER_TYPE_BRIDGE) {
  334. rc = pci_bus_read_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, &bcommand);
  335. bcommand |= PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR
  336. | PCI_BRIDGE_CTL_NO_ISA;
  337. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, bcommand);
  338. }
  339. }
  340. static int legacy_shpchprm_init_pci(void)
  341. {
  342. return 0;
  343. }
  344. int shpchprm_init(enum php_ctlr_type ctrl_type)
  345. {
  346. int retval;
  347. switch (ctrl_type) {
  348. case PCI:
  349. retval = legacy_shpchprm_init_pci();
  350. break;
  351. default:
  352. retval = -ENODEV;
  353. break;
  354. }
  355. return retval;
  356. }