Initial import from FreeBSD RELENG_4:
[games.git] / sys / boot / arc / lib / arch / alpha / rpb.c
1 /*-
2  * Copyright (c) 1998 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/boot/arc/lib/arch/alpha/rpb.c,v 1.2 1999/08/28 00:39:40 peter Exp $
27  */
28
29 #include <stand.h>
30 #include <machine/rpb.h>
31 #include "arctypes.h"
32 #include "arcfuncs.h"
33
34 struct rpb RPB = {
35     0,                          /* rpb_phys */
36     {"HWRPB"},                  /* rpb_magic */
37     HWRPB_DSRDB_MINVERS,        /* rpb_version */
38     sizeof(struct rpb),         /* rpb_size */
39     0,                          /* rpb_primary_cpu_id */
40     8192,                       /* rpb_page_size */
41     43,                         /* rpb_phys_addr_size */
42     0,                          /* rpb_max_asn */
43     {0},                        /* rpb_ssn */
44     ST_EB164,                   /* rpb_type */
45     SV_ST_ALPHAPC164LX_533,     /* rpb_variation */
46     {"0000"},                   /* rpb_revision */
47     1024*4096,                  /* rpb_intr_freq */
48     533*1024*1024,              /* rpb_cc_freq */
49     0,                          /* rpb_vptb */
50     0,                          /* rpb_reserved_arch */
51     0,                          /* rpb_tbhint_off */
52     0,                          /* rpb_pcs_cnt */
53     0,                          /* rpb_pcs_size */
54     0,                          /* rpb_pcs_off */
55     0,                          /* rpb_ctb_cnt */
56     0,                          /* rpb_ctb_size */
57     0,                          /* rpb_ctb_off */
58     0,                          /* rpb_crb_off */
59     0,                          /* rpb_memdat_off */
60     0,                          /* rpb_condat_off */
61     0,                          /* rpb_fru_off */
62     0,                          /* rpb_save_term */
63     0,                          /* rpb_save_term_val */
64     0,                          /* rpb_rest_term */
65     0,                          /* rpb_rest_term_val */
66     0,                          /* rpb_restart */
67     0,                          /* rpb_restart_val */
68     0,                          /* rpb_reserve_os */
69     0,                          /* rpb_reserve_hw */
70     0,                          /* rpb_checksum */
71     0,                          /* rpb_rxrdy */
72     0,                          /* rpb_txrdy */
73     0,                          /* rpb_dsrdb_off */
74     {0},                        /* rpb_rpb_tbhint */
75 };
76
77 #define ROUNDUP(x)      (((x) + sizeof(u_int64_t) - 1) \
78                          & ~(sizeof(u_int64_t) - 1))
79
80 u_int64_t
81 checksum(void *p, size_t size)
82 {
83     u_int64_t sum = 0;
84     u_int64_t *lp = (u_int64_t *)p;
85     int i;
86
87     printf("checksum(%p, %d)\n", p, size);
88     size = ROUNDUP(size) / sizeof(u_int64_t);
89     for (i = 0; i < size; i++)
90         sum += lp[i];
91 }
92
93 size_t
94 size_mddt()
95 {
96     int count = 0;
97     MEMORY_DESCRIPTOR *desc;
98
99     for (desc = GetMemoryDescriptor(NULL); desc;
100          desc = GetMemoryDescriptor(desc)) {
101         count++;
102     }
103
104     return ROUNDUP(sizeof(struct mddt)
105                    + (count - 1) * sizeof(struct mddt_cluster));
106 }
107
108 void
109 write_mddt(struct mddt *mddt, size_t size)
110 {
111     int count = 0, i;
112     MEMORY_DESCRIPTOR *desc;
113     u_int64_t *p;
114
115     memset(mddt, 0, sizeof(struct mddt));
116     for (desc = GetMemoryDescriptor(NULL); desc;
117          desc = GetMemoryDescriptor(desc)) {
118         struct mddt_cluster *mc;
119         mc = &mddt->mddt_clusters[count];
120         mc->mddt_pfn = desc->BasePage;
121         mc->mddt_pg_cnt = desc->PageCount;
122         mc->mddt_pg_test = 0;
123         mc->mddt_v_bitaddr = 0;
124         mc->mddt_p_bitaddr = 0;
125         mc->mddt_bit_cksum = 0;
126
127         /*
128          * Not sure about the FirmwareTemporary bit but my 164LX has
129          * about 60Mb marked this way.
130          */
131         if (desc->Type == MemoryFree || desc->Type == MemoryFirmwareTemporary)
132             mc->mddt_usage = MDDT_SYSTEM;
133         else if (desc->Type == MemorySpecialMemory)
134             mc->mddt_usage = MDDT_NONVOLATILE; /* ?? */
135         else
136             mc->mddt_usage = MDDT_PALCODE;
137         count++;
138     }
139     mddt->mddt_cluster_cnt = count;
140     mddt->mddt_cksum = checksum(mddt, size);
141 }
142
143 size_t
144 size_rpb()
145 {
146     return sizeof(struct rpb) + size_mddt();
147 }
148
149 void
150 write_rpb(struct rpb *rpb)
151 {
152     EXTENDED_SYSTEM_INFORMATION sysinfo;
153     SYSTEM_ID *sysid;
154
155     ReturnExtendedSystemInformation(&sysinfo);
156
157     memset(rpb, 0, sizeof(struct rpb));
158     rpb->rpb_phys = 0;                  /* XXX */
159     strcpy(rpb->rpb_magic, "HWRPB");
160     rpb->rpb_version = HWRPB_DSRDB_MINVERS;
161     rpb->rpb_size = sizeof(struct rpb);
162     rpb->rpb_primary_cpu_id = 0;        /* XXX */
163     rpb->rpb_page_size = sysinfo.ProcessorPageSize;
164     rpb->rpb_phys_addr_size = sysinfo.NumberOfPhysicalAddressBits;
165     rpb->rpb_max_asn = sysinfo.MaximumAddressSpaceNumber;
166     rpb->rpb_type = ST_EB164;           /* XXX */
167     rpb->rpb_variation = SV_ST_ALPHAPC164LX_533; /* XXX */
168     rpb->rpb_intr_freq = 1024*4096;     /* XXX */
169     rpb->rpb_cc_freq = 533000000;       /* XXX */
170     rpb->rpb_memdat_off = sizeof(struct rpb);
171     write_mddt((struct mddt *)((caddr_t) rpb + rpb->rpb_memdat_off),
172                size_mddt());
173     rpb->rpb_checksum = checksum(rpb, 280); /* only sum first 280 bytes */
174 }
175
176 struct rpb *
177 make_rpb()
178 {
179     EXTENDED_SYSTEM_INFORMATION sysinfo;
180     struct rpb *rpb;
181
182     ReturnExtendedSystemInformation(&sysinfo);
183     printf("sysinfo.ProcessorId = %x\n", sysinfo.ProcessorId);
184     printf("sysinfo.ProcessorRevision = %d\n", sysinfo.ProcessorRevision);
185     printf("sysinfo.ProcessorPageSize = %d\n", sysinfo.ProcessorPageSize);
186     printf("sysinfo.NumberOfPhysicalAddressBits = %d\n", sysinfo.NumberOfPhysicalAddressBits);
187     printf("sysinfo.MaximumAddressSpaceNumber = %d\n", sysinfo.MaximumAddressSpaceNumber);
188     printf("sysinfo.ProcessorCycleCounterPeriod = %d\n", sysinfo.ProcessorCycleCounterPeriod);
189     printf("sysinfo.SystemRevision = %d\n", sysinfo.SystemRevision);
190     printf("sysinfo.SystemSerialNumber = %s\n", sysinfo.SystemSerialNumber);
191     printf("sysinfo.FirmwareVersion = %s\n", sysinfo.FirmwareVersion);
192
193     rpb = malloc(size_rpb());
194     write_rpb(rpb);
195     return rpb;
196 }