cpqphp_nvram.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * Compaq Hot Plug Controller Driver
  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. *
  8. * All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  18. * NON INFRINGEMENT. See the GNU General Public License for more
  19. * details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. * Send feedback to <greg@kroah.com>
  26. *
  27. */
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/types.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/slab.h>
  33. #include <linux/workqueue.h>
  34. #include <linux/pci.h>
  35. #include <linux/init.h>
  36. #include <asm/uaccess.h>
  37. #include "cpqphp.h"
  38. #include "cpqphp_nvram.h"
  39. #define ROM_INT15_PHY_ADDR 0x0FF859
  40. #define READ_EV 0xD8A4
  41. #define WRITE_EV 0xD8A5
  42. struct register_foo {
  43. union {
  44. unsigned long lword; /* eax */
  45. unsigned short word; /* ax */
  46. struct {
  47. unsigned char low; /* al */
  48. unsigned char high; /* ah */
  49. } byte;
  50. } data;
  51. unsigned char opcode; /* see below */
  52. unsigned long length; /* if the reg. is a pointer, how much data */
  53. } __attribute__ ((packed));
  54. struct all_reg {
  55. struct register_foo eax_reg;
  56. struct register_foo ebx_reg;
  57. struct register_foo ecx_reg;
  58. struct register_foo edx_reg;
  59. struct register_foo edi_reg;
  60. struct register_foo esi_reg;
  61. struct register_foo eflags_reg;
  62. } __attribute__ ((packed));
  63. struct ev_hrt_header {
  64. u8 Version;
  65. u8 num_of_ctrl;
  66. u8 next;
  67. };
  68. struct ev_hrt_ctrl {
  69. u8 bus;
  70. u8 device;
  71. u8 function;
  72. u8 mem_avail;
  73. u8 p_mem_avail;
  74. u8 io_avail;
  75. u8 bus_avail;
  76. u8 next;
  77. };
  78. static u8 evbuffer_init;
  79. static u8 evbuffer_length;
  80. static u8 evbuffer[1024];
  81. static void __iomem *compaq_int15_entry_point;
  82. static spinlock_t int15_lock; /* lock for ordering int15_bios_call() */
  83. /* This is a series of function that deals with
  84. setting & getting the hotplug resource table in some environment variable.
  85. */
  86. /*
  87. * We really shouldn't be doing this unless there is a _very_ good reason to!!!
  88. * greg k-h
  89. */
  90. static u32 add_byte( u32 **p_buffer, u8 value, u32 *used, u32 *avail)
  91. {
  92. u8 **tByte;
  93. if ((*used + 1) > *avail)
  94. return(1);
  95. *((u8*)*p_buffer) = value;
  96. tByte = (u8**)p_buffer;
  97. (*tByte)++;
  98. *used+=1;
  99. return(0);
  100. }
  101. static u32 add_dword( u32 **p_buffer, u32 value, u32 *used, u32 *avail)
  102. {
  103. if ((*used + 4) > *avail)
  104. return(1);
  105. **p_buffer = value;
  106. (*p_buffer)++;
  107. *used+=4;
  108. return(0);
  109. }
  110. /*
  111. * check_for_compaq_ROM
  112. *
  113. * this routine verifies that the ROM OEM string is 'COMPAQ'
  114. *
  115. * returns 0 for non-Compaq ROM, 1 for Compaq ROM
  116. */
  117. static int check_for_compaq_ROM (void __iomem *rom_start)
  118. {
  119. u8 temp1, temp2, temp3, temp4, temp5, temp6;
  120. int result = 0;
  121. temp1 = readb(rom_start + 0xffea + 0);
  122. temp2 = readb(rom_start + 0xffea + 1);
  123. temp3 = readb(rom_start + 0xffea + 2);
  124. temp4 = readb(rom_start + 0xffea + 3);
  125. temp5 = readb(rom_start + 0xffea + 4);
  126. temp6 = readb(rom_start + 0xffea + 5);
  127. if ((temp1 == 'C') &&
  128. (temp2 == 'O') &&
  129. (temp3 == 'M') &&
  130. (temp4 == 'P') &&
  131. (temp5 == 'A') &&
  132. (temp6 == 'Q')) {
  133. result = 1;
  134. }
  135. dbg ("%s - returned %d\n", __FUNCTION__, result);
  136. return result;
  137. }
  138. static u32 access_EV (u16 operation, u8 *ev_name, u8 *buffer, u32 *buf_size)
  139. {
  140. unsigned long flags;
  141. int op = operation;
  142. int ret_val;
  143. if (!compaq_int15_entry_point)
  144. return -ENODEV;
  145. spin_lock_irqsave(&int15_lock, flags);
  146. __asm__ (
  147. "xorl %%ebx,%%ebx\n" \
  148. "xorl %%edx,%%edx\n" \
  149. "pushf\n" \
  150. "push %%cs\n" \
  151. "cli\n" \
  152. "call *%6\n"
  153. : "=c" (*buf_size), "=a" (ret_val)
  154. : "a" (op), "c" (*buf_size), "S" (ev_name),
  155. "D" (buffer), "m" (compaq_int15_entry_point)
  156. : "%ebx", "%edx");
  157. spin_unlock_irqrestore(&int15_lock, flags);
  158. return((ret_val & 0xFF00) >> 8);
  159. }
  160. /*
  161. * load_HRT
  162. *
  163. * Read the hot plug Resource Table from NVRAM
  164. */
  165. static int load_HRT (void __iomem *rom_start)
  166. {
  167. u32 available;
  168. u32 temp_dword;
  169. u8 temp_byte = 0xFF;
  170. u32 rc;
  171. if (!check_for_compaq_ROM(rom_start)) {
  172. return -ENODEV;
  173. }
  174. available = 1024;
  175. // Now load the EV
  176. temp_dword = available;
  177. rc = access_EV(READ_EV, "CQTHPS", evbuffer, &temp_dword);
  178. evbuffer_length = temp_dword;
  179. // We're maintaining the resource lists so write FF to invalidate old info
  180. temp_dword = 1;
  181. rc = access_EV(WRITE_EV, "CQTHPS", &temp_byte, &temp_dword);
  182. return rc;
  183. }
  184. /*
  185. * store_HRT
  186. *
  187. * Save the hot plug Resource Table in NVRAM
  188. */
  189. static u32 store_HRT (void __iomem *rom_start)
  190. {
  191. u32 *buffer;
  192. u32 *pFill;
  193. u32 usedbytes;
  194. u32 available;
  195. u32 temp_dword;
  196. u32 rc;
  197. u8 loop;
  198. u8 numCtrl = 0;
  199. struct controller *ctrl;
  200. struct pci_resource *resNode;
  201. struct ev_hrt_header *p_EV_header;
  202. struct ev_hrt_ctrl *p_ev_ctrl;
  203. available = 1024;
  204. if (!check_for_compaq_ROM(rom_start)) {
  205. return(1);
  206. }
  207. buffer = (u32*) evbuffer;
  208. if (!buffer)
  209. return(1);
  210. pFill = buffer;
  211. usedbytes = 0;
  212. p_EV_header = (struct ev_hrt_header *) pFill;
  213. ctrl = cpqhp_ctrl_list;
  214. // The revision of this structure
  215. rc = add_byte( &pFill, 1 + ctrl->push_flag, &usedbytes, &available);
  216. if (rc)
  217. return(rc);
  218. // The number of controllers
  219. rc = add_byte( &pFill, 1, &usedbytes, &available);
  220. if (rc)
  221. return(rc);
  222. while (ctrl) {
  223. p_ev_ctrl = (struct ev_hrt_ctrl *) pFill;
  224. numCtrl++;
  225. // The bus number
  226. rc = add_byte( &pFill, ctrl->bus, &usedbytes, &available);
  227. if (rc)
  228. return(rc);
  229. // The device Number
  230. rc = add_byte( &pFill, PCI_SLOT(ctrl->pci_dev->devfn), &usedbytes, &available);
  231. if (rc)
  232. return(rc);
  233. // The function Number
  234. rc = add_byte( &pFill, PCI_FUNC(ctrl->pci_dev->devfn), &usedbytes, &available);
  235. if (rc)
  236. return(rc);
  237. // Skip the number of available entries
  238. rc = add_dword( &pFill, 0, &usedbytes, &available);
  239. if (rc)
  240. return(rc);
  241. // Figure out memory Available
  242. resNode = ctrl->mem_head;
  243. loop = 0;
  244. while (resNode) {
  245. loop ++;
  246. // base
  247. rc = add_dword( &pFill, resNode->base, &usedbytes, &available);
  248. if (rc)
  249. return(rc);
  250. // length
  251. rc = add_dword( &pFill, resNode->length, &usedbytes, &available);
  252. if (rc)
  253. return(rc);
  254. resNode = resNode->next;
  255. }
  256. // Fill in the number of entries
  257. p_ev_ctrl->mem_avail = loop;
  258. // Figure out prefetchable memory Available
  259. resNode = ctrl->p_mem_head;
  260. loop = 0;
  261. while (resNode) {
  262. loop ++;
  263. // base
  264. rc = add_dword( &pFill, resNode->base, &usedbytes, &available);
  265. if (rc)
  266. return(rc);
  267. // length
  268. rc = add_dword( &pFill, resNode->length, &usedbytes, &available);
  269. if (rc)
  270. return(rc);
  271. resNode = resNode->next;
  272. }
  273. // Fill in the number of entries
  274. p_ev_ctrl->p_mem_avail = loop;
  275. // Figure out IO Available
  276. resNode = ctrl->io_head;
  277. loop = 0;
  278. while (resNode) {
  279. loop ++;
  280. // base
  281. rc = add_dword( &pFill, resNode->base, &usedbytes, &available);
  282. if (rc)
  283. return(rc);
  284. // length
  285. rc = add_dword( &pFill, resNode->length, &usedbytes, &available);
  286. if (rc)
  287. return(rc);
  288. resNode = resNode->next;
  289. }
  290. // Fill in the number of entries
  291. p_ev_ctrl->io_avail = loop;
  292. // Figure out bus Available
  293. resNode = ctrl->bus_head;
  294. loop = 0;
  295. while (resNode) {
  296. loop ++;
  297. // base
  298. rc = add_dword( &pFill, resNode->base, &usedbytes, &available);
  299. if (rc)
  300. return(rc);
  301. // length
  302. rc = add_dword( &pFill, resNode->length, &usedbytes, &available);
  303. if (rc)
  304. return(rc);
  305. resNode = resNode->next;
  306. }
  307. // Fill in the number of entries
  308. p_ev_ctrl->bus_avail = loop;
  309. ctrl = ctrl->next;
  310. }
  311. p_EV_header->num_of_ctrl = numCtrl;
  312. // Now store the EV
  313. temp_dword = usedbytes;
  314. rc = access_EV(WRITE_EV, "CQTHPS", (u8*) buffer, &temp_dword);
  315. dbg("usedbytes = 0x%x, length = 0x%x\n", usedbytes, temp_dword);
  316. evbuffer_length = temp_dword;
  317. if (rc) {
  318. err(msg_unable_to_save);
  319. return(1);
  320. }
  321. return(0);
  322. }
  323. void compaq_nvram_init (void __iomem *rom_start)
  324. {
  325. if (rom_start) {
  326. compaq_int15_entry_point = (rom_start + ROM_INT15_PHY_ADDR - ROM_PHY_ADDR);
  327. }
  328. dbg("int15 entry = %p\n", compaq_int15_entry_point);
  329. /* initialize our int15 lock */
  330. spin_lock_init(&int15_lock);
  331. }
  332. int compaq_nvram_load (void __iomem *rom_start, struct controller *ctrl)
  333. {
  334. u8 bus, device, function;
  335. u8 nummem, numpmem, numio, numbus;
  336. u32 rc;
  337. u8 *p_byte;
  338. struct pci_resource *mem_node;
  339. struct pci_resource *p_mem_node;
  340. struct pci_resource *io_node;
  341. struct pci_resource *bus_node;
  342. struct ev_hrt_ctrl *p_ev_ctrl;
  343. struct ev_hrt_header *p_EV_header;
  344. if (!evbuffer_init) {
  345. // Read the resource list information in from NVRAM
  346. if (load_HRT(rom_start))
  347. memset (evbuffer, 0, 1024);
  348. evbuffer_init = 1;
  349. }
  350. // If we saved information in NVRAM, use it now
  351. p_EV_header = (struct ev_hrt_header *) evbuffer;
  352. // The following code is for systems where version 1.0 of this
  353. // driver has been loaded, but doesn't support the hardware.
  354. // In that case, the driver would incorrectly store something
  355. // in NVRAM.
  356. if ((p_EV_header->Version == 2) ||
  357. ((p_EV_header->Version == 1) && !ctrl->push_flag)) {
  358. p_byte = &(p_EV_header->next);
  359. p_ev_ctrl = (struct ev_hrt_ctrl *) &(p_EV_header->next);
  360. p_byte += 3;
  361. if (p_byte > ((u8*)p_EV_header + evbuffer_length))
  362. return 2;
  363. bus = p_ev_ctrl->bus;
  364. device = p_ev_ctrl->device;
  365. function = p_ev_ctrl->function;
  366. while ((bus != ctrl->bus) ||
  367. (device != PCI_SLOT(ctrl->pci_dev->devfn)) ||
  368. (function != PCI_FUNC(ctrl->pci_dev->devfn))) {
  369. nummem = p_ev_ctrl->mem_avail;
  370. numpmem = p_ev_ctrl->p_mem_avail;
  371. numio = p_ev_ctrl->io_avail;
  372. numbus = p_ev_ctrl->bus_avail;
  373. p_byte += 4;
  374. if (p_byte > ((u8*)p_EV_header + evbuffer_length))
  375. return 2;
  376. // Skip forward to the next entry
  377. p_byte += (nummem + numpmem + numio + numbus) * 8;
  378. if (p_byte > ((u8*)p_EV_header + evbuffer_length))
  379. return 2;
  380. p_ev_ctrl = (struct ev_hrt_ctrl *) p_byte;
  381. p_byte += 3;
  382. if (p_byte > ((u8*)p_EV_header + evbuffer_length))
  383. return 2;
  384. bus = p_ev_ctrl->bus;
  385. device = p_ev_ctrl->device;
  386. function = p_ev_ctrl->function;
  387. }
  388. nummem = p_ev_ctrl->mem_avail;
  389. numpmem = p_ev_ctrl->p_mem_avail;
  390. numio = p_ev_ctrl->io_avail;
  391. numbus = p_ev_ctrl->bus_avail;
  392. p_byte += 4;
  393. if (p_byte > ((u8*)p_EV_header + evbuffer_length))
  394. return 2;
  395. while (nummem--) {
  396. mem_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  397. if (!mem_node)
  398. break;
  399. mem_node->base = *(u32*)p_byte;
  400. dbg("mem base = %8.8x\n",mem_node->base);
  401. p_byte += 4;
  402. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  403. kfree(mem_node);
  404. return 2;
  405. }
  406. mem_node->length = *(u32*)p_byte;
  407. dbg("mem length = %8.8x\n",mem_node->length);
  408. p_byte += 4;
  409. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  410. kfree(mem_node);
  411. return 2;
  412. }
  413. mem_node->next = ctrl->mem_head;
  414. ctrl->mem_head = mem_node;
  415. }
  416. while (numpmem--) {
  417. p_mem_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  418. if (!p_mem_node)
  419. break;
  420. p_mem_node->base = *(u32*)p_byte;
  421. dbg("pre-mem base = %8.8x\n",p_mem_node->base);
  422. p_byte += 4;
  423. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  424. kfree(p_mem_node);
  425. return 2;
  426. }
  427. p_mem_node->length = *(u32*)p_byte;
  428. dbg("pre-mem length = %8.8x\n",p_mem_node->length);
  429. p_byte += 4;
  430. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  431. kfree(p_mem_node);
  432. return 2;
  433. }
  434. p_mem_node->next = ctrl->p_mem_head;
  435. ctrl->p_mem_head = p_mem_node;
  436. }
  437. while (numio--) {
  438. io_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  439. if (!io_node)
  440. break;
  441. io_node->base = *(u32*)p_byte;
  442. dbg("io base = %8.8x\n",io_node->base);
  443. p_byte += 4;
  444. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  445. kfree(io_node);
  446. return 2;
  447. }
  448. io_node->length = *(u32*)p_byte;
  449. dbg("io length = %8.8x\n",io_node->length);
  450. p_byte += 4;
  451. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  452. kfree(io_node);
  453. return 2;
  454. }
  455. io_node->next = ctrl->io_head;
  456. ctrl->io_head = io_node;
  457. }
  458. while (numbus--) {
  459. bus_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  460. if (!bus_node)
  461. break;
  462. bus_node->base = *(u32*)p_byte;
  463. p_byte += 4;
  464. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  465. kfree(bus_node);
  466. return 2;
  467. }
  468. bus_node->length = *(u32*)p_byte;
  469. p_byte += 4;
  470. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  471. kfree(bus_node);
  472. return 2;
  473. }
  474. bus_node->next = ctrl->bus_head;
  475. ctrl->bus_head = bus_node;
  476. }
  477. // If all of the following fail, we don't have any resources for
  478. // hot plug add
  479. rc = 1;
  480. rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
  481. rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
  482. rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
  483. rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
  484. if (rc)
  485. return(rc);
  486. } else {
  487. if ((evbuffer[0] != 0) && (!ctrl->push_flag))
  488. return 1;
  489. }
  490. return 0;
  491. }
  492. int compaq_nvram_store (void __iomem *rom_start)
  493. {
  494. int rc = 1;
  495. if (rom_start == NULL)
  496. return -ENODEV;
  497. if (evbuffer_init) {
  498. rc = store_HRT(rom_start);
  499. if (rc) {
  500. err(msg_unable_to_save);
  501. }
  502. }
  503. return rc;
  504. }