1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/console.h>
4 #include <intelblocks/gpmr.h>
5 #include <intelblocks/ioc.h>
6 #include <intelblocks/pcr.h>
7 #include <soc/pcr_ids.h>
8 #include <types.h>
9
10 /* GPMR Register read given offset */
gpmr_read32(uint16_t offset)11 uint32_t gpmr_read32(uint16_t offset)
12 {
13 if (CONFIG(SOC_INTEL_COMMON_BLOCK_IOC))
14 return ioc_reg_read32(offset);
15 else
16 return pcr_read32(PID_DMI, offset);
17 }
18
19 /* GPMR Register write given offset and val */
gpmr_write32(uint16_t offset,uint32_t val)20 void gpmr_write32(uint16_t offset, uint32_t val)
21 {
22 if (CONFIG(SOC_INTEL_COMMON_BLOCK_IOC))
23 return ioc_reg_write32(offset, val);
24 else
25 return pcr_write32(PID_DMI, offset, val);
26 }
27
gpmr_or32(uint16_t offset,uint32_t ordata)28 void gpmr_or32(uint16_t offset, uint32_t ordata)
29 {
30 if (CONFIG(SOC_INTEL_COMMON_BLOCK_IOC))
31 return ioc_reg_or32(offset, ordata);
32 else
33 return pcr_or32(PID_DMI, offset, ordata);
34 }
35
36 /* Check for available free gpmr */
get_available_gpmr(void)37 static int get_available_gpmr(void)
38 {
39 int i;
40 uint32_t val;
41
42 for (i = 0; i < MAX_GPMR_REGS; i++) {
43 val = gpmr_read32(GPMR_DID_OFFSET(i));
44 if (!(val & GPMR_EN))
45 return i;
46 }
47 printk(BIOS_ERR, "%s: No available free gpmr found\n", __func__);
48 return CB_ERR;
49 }
50
51 /* Configure GPMR for the given base and size of extended BIOS Region */
enable_gpmr(uint32_t base,uint32_t size,uint32_t dest_id)52 enum cb_err enable_gpmr(uint32_t base, uint32_t size, uint32_t dest_id)
53 {
54 int gpmr_num;
55 uint32_t limit;
56
57 if (base & ~(GPMR_BASE_MASK << GPMR_BASE_SHIFT)) {
58 printk(BIOS_ERR, "base is not 64-KiB aligned!\n");
59 return CB_ERR;
60 }
61
62 limit = base + (size - 1);
63
64 if (limit < base) {
65 printk(BIOS_ERR, "Invalid limit: limit cannot be less than base!\n");
66 return CB_ERR;
67 }
68
69 if ((limit & ~GPMR_LIMIT_MASK) != 0xffff) {
70 printk(BIOS_ERR, "limit does not end on a 64-KiB boundary!\n");
71 return CB_ERR;
72 }
73
74 /* Get available free GPMR */
75 gpmr_num = get_available_gpmr();
76 if (gpmr_num == CB_ERR)
77 return CB_ERR;
78
79 /* Program Range for the given decode window */
80 gpmr_write32(GPMR_OFFSET(gpmr_num), (limit & GPMR_LIMIT_MASK) |
81 ((base >> GPMR_BASE_SHIFT) & GPMR_BASE_MASK));
82
83 /* Program source decode enable bit and the Destination ID */
84 gpmr_write32(GPMR_DID_OFFSET(gpmr_num), dest_id | GPMR_EN);
85
86 return CB_SUCCESS;
87 }
88