1 // SPDX-License-Identifier: GPL-2.0+
3 * Compaq Hot Plug Controller Driver
5 * Copyright (C) 1995,2001 Compaq Computer Corporation
6 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
7 * Copyright (C) 2001 IBM Corp.
11 * Send feedback to <greg@kroah.com>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/proc_fs.h>
19 #include <linux/slab.h>
20 #include <linux/workqueue.h>
21 #include <linux/pci.h>
22 #include <linux/pci_hotplug.h>
23 #include <linux/uaccess.h>
25 #include "cpqphp_nvram.h"
28 #define ROM_INT15_PHY_ADDR 0x0FF859
29 #define READ_EV 0xD8A4
30 #define WRITE_EV 0xD8A5
34 unsigned long lword
; /* eax */
35 unsigned short word
; /* ax */
38 unsigned char low
; /* al */
39 unsigned char high
; /* ah */
43 unsigned char opcode
; /* see below */
44 unsigned long length
; /* if the reg. is a pointer, how much data */
45 } __attribute__ ((packed
));
48 struct register_foo eax_reg
;
49 struct register_foo ebx_reg
;
50 struct register_foo ecx_reg
;
51 struct register_foo edx_reg
;
52 struct register_foo edi_reg
;
53 struct register_foo esi_reg
;
54 struct register_foo eflags_reg
;
55 } __attribute__ ((packed
));
58 struct ev_hrt_header
{
76 static u8 evbuffer_init
;
77 static u8 evbuffer_length
;
78 static u8 evbuffer
[1024];
80 static void __iomem
*compaq_int15_entry_point
;
82 /* lock for ordering int15_bios_call() */
83 static spinlock_t int15_lock
;
86 /* This is a series of function that deals with
87 * setting & getting the hotplug resource table in some environment variable.
91 * We really shouldn't be doing this unless there is a _very_ good reason to!!!
96 static u32
add_byte(u32
**p_buffer
, u8 value
, u32
*used
, u32
*avail
)
100 if ((*used
+ 1) > *avail
)
103 *((u8
*)*p_buffer
) = value
;
104 tByte
= (u8
**)p_buffer
;
111 static u32
add_dword(u32
**p_buffer
, u32 value
, u32
*used
, u32
*avail
)
113 if ((*used
+ 4) > *avail
)
124 * check_for_compaq_ROM
126 * this routine verifies that the ROM OEM string is 'COMPAQ'
128 * returns 0 for non-Compaq ROM, 1 for Compaq ROM
130 static int check_for_compaq_ROM(void __iomem
*rom_start
)
132 u8 temp1
, temp2
, temp3
, temp4
, temp5
, temp6
;
135 temp1
= readb(rom_start
+ 0xffea + 0);
136 temp2
= readb(rom_start
+ 0xffea + 1);
137 temp3
= readb(rom_start
+ 0xffea + 2);
138 temp4
= readb(rom_start
+ 0xffea + 3);
139 temp5
= readb(rom_start
+ 0xffea + 4);
140 temp6
= readb(rom_start
+ 0xffea + 5);
141 if ((temp1
== 'C') &&
149 dbg("%s - returned %d\n", __func__
, result
);
154 static u32
access_EV(u16 operation
, u8
*ev_name
, u8
*buffer
, u32
*buf_size
)
160 if (!compaq_int15_entry_point
)
163 spin_lock_irqsave(&int15_lock
, flags
);
165 "xorl %%ebx,%%ebx\n" \
166 "xorl %%edx,%%edx\n" \
171 : "=c" (*buf_size
), "=a" (ret_val
)
172 : "a" (op
), "c" (*buf_size
), "S" (ev_name
),
173 "D" (buffer
), "m" (compaq_int15_entry_point
)
175 spin_unlock_irqrestore(&int15_lock
, flags
);
177 return((ret_val
& 0xFF00) >> 8);
184 * Read the hot plug Resource Table from NVRAM
186 static int load_HRT(void __iomem
*rom_start
)
193 if (!check_for_compaq_ROM(rom_start
))
198 /* Now load the EV */
199 temp_dword
= available
;
201 rc
= access_EV(READ_EV
, "CQTHPS", evbuffer
, &temp_dword
);
203 evbuffer_length
= temp_dword
;
205 /* We're maintaining the resource lists so write FF to invalidate old
210 rc
= access_EV(WRITE_EV
, "CQTHPS", &temp_byte
, &temp_dword
);
219 * Save the hot plug Resource Table in NVRAM
221 static u32
store_HRT(void __iomem
*rom_start
)
231 struct controller
*ctrl
;
232 struct pci_resource
*resNode
;
233 struct ev_hrt_header
*p_EV_header
;
234 struct ev_hrt_ctrl
*p_ev_ctrl
;
238 if (!check_for_compaq_ROM(rom_start
))
241 buffer
= (u32
*) evbuffer
;
249 p_EV_header
= (struct ev_hrt_header
*) pFill
;
251 ctrl
= cpqhp_ctrl_list
;
253 /* The revision of this structure */
254 rc
= add_byte(&pFill
, 1 + ctrl
->push_flag
, &usedbytes
, &available
);
258 /* The number of controllers */
259 rc
= add_byte(&pFill
, 1, &usedbytes
, &available
);
264 p_ev_ctrl
= (struct ev_hrt_ctrl
*) pFill
;
269 rc
= add_byte(&pFill
, ctrl
->bus
, &usedbytes
, &available
);
273 /* The device Number */
274 rc
= add_byte(&pFill
, PCI_SLOT(ctrl
->pci_dev
->devfn
), &usedbytes
, &available
);
278 /* The function Number */
279 rc
= add_byte(&pFill
, PCI_FUNC(ctrl
->pci_dev
->devfn
), &usedbytes
, &available
);
283 /* Skip the number of available entries */
284 rc
= add_dword(&pFill
, 0, &usedbytes
, &available
);
288 /* Figure out memory Available */
290 resNode
= ctrl
->mem_head
;
298 rc
= add_dword(&pFill
, resNode
->base
, &usedbytes
, &available
);
303 rc
= add_dword(&pFill
, resNode
->length
, &usedbytes
, &available
);
307 resNode
= resNode
->next
;
310 /* Fill in the number of entries */
311 p_ev_ctrl
->mem_avail
= loop
;
313 /* Figure out prefetchable memory Available */
315 resNode
= ctrl
->p_mem_head
;
323 rc
= add_dword(&pFill
, resNode
->base
, &usedbytes
, &available
);
328 rc
= add_dword(&pFill
, resNode
->length
, &usedbytes
, &available
);
332 resNode
= resNode
->next
;
335 /* Fill in the number of entries */
336 p_ev_ctrl
->p_mem_avail
= loop
;
338 /* Figure out IO Available */
340 resNode
= ctrl
->io_head
;
348 rc
= add_dword(&pFill
, resNode
->base
, &usedbytes
, &available
);
353 rc
= add_dword(&pFill
, resNode
->length
, &usedbytes
, &available
);
357 resNode
= resNode
->next
;
360 /* Fill in the number of entries */
361 p_ev_ctrl
->io_avail
= loop
;
363 /* Figure out bus Available */
365 resNode
= ctrl
->bus_head
;
373 rc
= add_dword(&pFill
, resNode
->base
, &usedbytes
, &available
);
378 rc
= add_dword(&pFill
, resNode
->length
, &usedbytes
, &available
);
382 resNode
= resNode
->next
;
385 /* Fill in the number of entries */
386 p_ev_ctrl
->bus_avail
= loop
;
391 p_EV_header
->num_of_ctrl
= numCtrl
;
393 /* Now store the EV */
395 temp_dword
= usedbytes
;
397 rc
= access_EV(WRITE_EV
, "CQTHPS", (u8
*) buffer
, &temp_dword
);
399 dbg("usedbytes = 0x%x, length = 0x%x\n", usedbytes
, temp_dword
);
401 evbuffer_length
= temp_dword
;
404 err(msg_unable_to_save
);
412 void compaq_nvram_init(void __iomem
*rom_start
)
415 compaq_int15_entry_point
= (rom_start
+ ROM_INT15_PHY_ADDR
- ROM_PHY_ADDR
);
417 dbg("int15 entry = %p\n", compaq_int15_entry_point
);
419 /* initialize our int15 lock */
420 spin_lock_init(&int15_lock
);
424 int compaq_nvram_load(void __iomem
*rom_start
, struct controller
*ctrl
)
426 u8 bus
, device
, function
;
427 u8 nummem
, numpmem
, numio
, numbus
;
430 struct pci_resource
*mem_node
;
431 struct pci_resource
*p_mem_node
;
432 struct pci_resource
*io_node
;
433 struct pci_resource
*bus_node
;
434 struct ev_hrt_ctrl
*p_ev_ctrl
;
435 struct ev_hrt_header
*p_EV_header
;
437 if (!evbuffer_init
) {
438 /* Read the resource list information in from NVRAM */
439 if (load_HRT(rom_start
))
440 memset(evbuffer
, 0, 1024);
445 /* If we saved information in NVRAM, use it now */
446 p_EV_header
= (struct ev_hrt_header
*) evbuffer
;
448 /* The following code is for systems where version 1.0 of this
449 * driver has been loaded, but doesn't support the hardware.
450 * In that case, the driver would incorrectly store something
453 if ((p_EV_header
->Version
== 2) ||
454 ((p_EV_header
->Version
== 1) && !ctrl
->push_flag
)) {
455 p_byte
= &(p_EV_header
->next
);
457 p_ev_ctrl
= (struct ev_hrt_ctrl
*) &(p_EV_header
->next
);
461 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
))
464 bus
= p_ev_ctrl
->bus
;
465 device
= p_ev_ctrl
->device
;
466 function
= p_ev_ctrl
->function
;
468 while ((bus
!= ctrl
->bus
) ||
469 (device
!= PCI_SLOT(ctrl
->pci_dev
->devfn
)) ||
470 (function
!= PCI_FUNC(ctrl
->pci_dev
->devfn
))) {
471 nummem
= p_ev_ctrl
->mem_avail
;
472 numpmem
= p_ev_ctrl
->p_mem_avail
;
473 numio
= p_ev_ctrl
->io_avail
;
474 numbus
= p_ev_ctrl
->bus_avail
;
478 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
))
481 /* Skip forward to the next entry */
482 p_byte
+= (nummem
+ numpmem
+ numio
+ numbus
) * 8;
484 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
))
487 p_ev_ctrl
= (struct ev_hrt_ctrl
*) p_byte
;
491 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
))
494 bus
= p_ev_ctrl
->bus
;
495 device
= p_ev_ctrl
->device
;
496 function
= p_ev_ctrl
->function
;
499 nummem
= p_ev_ctrl
->mem_avail
;
500 numpmem
= p_ev_ctrl
->p_mem_avail
;
501 numio
= p_ev_ctrl
->io_avail
;
502 numbus
= p_ev_ctrl
->bus_avail
;
506 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
))
510 mem_node
= kmalloc(sizeof(struct pci_resource
), GFP_KERNEL
);
515 mem_node
->base
= *(u32
*)p_byte
;
516 dbg("mem base = %8.8x\n", mem_node
->base
);
519 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
)) {
524 mem_node
->length
= *(u32
*)p_byte
;
525 dbg("mem length = %8.8x\n", mem_node
->length
);
528 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
)) {
533 mem_node
->next
= ctrl
->mem_head
;
534 ctrl
->mem_head
= mem_node
;
538 p_mem_node
= kmalloc(sizeof(struct pci_resource
), GFP_KERNEL
);
543 p_mem_node
->base
= *(u32
*)p_byte
;
544 dbg("pre-mem base = %8.8x\n", p_mem_node
->base
);
547 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
)) {
552 p_mem_node
->length
= *(u32
*)p_byte
;
553 dbg("pre-mem length = %8.8x\n", p_mem_node
->length
);
556 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
)) {
561 p_mem_node
->next
= ctrl
->p_mem_head
;
562 ctrl
->p_mem_head
= p_mem_node
;
566 io_node
= kmalloc(sizeof(struct pci_resource
), GFP_KERNEL
);
571 io_node
->base
= *(u32
*)p_byte
;
572 dbg("io base = %8.8x\n", io_node
->base
);
575 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
)) {
580 io_node
->length
= *(u32
*)p_byte
;
581 dbg("io length = %8.8x\n", io_node
->length
);
584 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
)) {
589 io_node
->next
= ctrl
->io_head
;
590 ctrl
->io_head
= io_node
;
594 bus_node
= kmalloc(sizeof(struct pci_resource
), GFP_KERNEL
);
599 bus_node
->base
= *(u32
*)p_byte
;
602 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
)) {
607 bus_node
->length
= *(u32
*)p_byte
;
610 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
)) {
615 bus_node
->next
= ctrl
->bus_head
;
616 ctrl
->bus_head
= bus_node
;
619 /* If all of the following fail, we don't have any resources for
623 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->mem_head
));
624 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->p_mem_head
));
625 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->io_head
));
626 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->bus_head
));
631 if ((evbuffer
[0] != 0) && (!ctrl
->push_flag
))
639 int compaq_nvram_store(void __iomem
*rom_start
)
643 if (rom_start
== NULL
)
647 rc
= store_HRT(rom_start
);
649 err(msg_unable_to_save
);