Fully synchronize sys/boot from FreeBSD-5.x, but add / to the module path
[dragonfly.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  * $DragonFly: src/sys/boot/arc/lib/arch/alpha/Attic/rpb.c,v 1.3 2003/11/10 06:08:31 dillon Exp $
28  */
29
30 #include <stand.h>
31 #include <machine/rpb.h>
32 #include "arctypes.h"
33 #include "arcfuncs.h"
34
35 struct rpb RPB = {
36     0,                          /* rpb_phys */
37     {"HWRPB"},                  /* rpb_magic */
38     HWRPB_DSRDB_MINVERS,        /* rpb_version */
39     sizeof(struct rpb),         /* rpb_size */
40     0,                          /* rpb_primary_cpu_id */
41     8192,                       /* rpb_page_size */
42     43,                         /* rpb_phys_addr_size */
43     0,                          /* rpb_max_asn */
44     {0},                        /* rpb_ssn */
45     ST_EB164,                   /* rpb_type */
46     SV_ST_ALPHAPC164LX_533,     /* rpb_variation */
47     {"0000"},                   /* rpb_revision */
48     1024*4096,                  /* rpb_intr_freq */
49     533*1024*1024,              /* rpb_cc_freq */
50     0,                          /* rpb_vptb */
51     0,                          /* rpb_reserved_arch */
52     0,                          /* rpb_tbhint_off */
53     0,                          /* rpb_pcs_cnt */
54     0,                          /* rpb_pcs_size */
55     0,                          /* rpb_pcs_off */
56     0,                          /* rpb_ctb_cnt */
57     0,                          /* rpb_ctb_size */
58     0,                          /* rpb_ctb_off */
59     0,                          /* rpb_crb_off */
60     0,                          /* rpb_memdat_off */
61     0,                          /* rpb_condat_off */
62     0,                          /* rpb_fru_off */
63     0,                          /* rpb_save_term */
64     0,                          /* rpb_save_term_val */
65     0,                          /* rpb_rest_term */
66     0,                          /* rpb_rest_term_val */
67     0,                          /* rpb_restart */
68     0,                          /* rpb_restart_val */
69     0,                          /* rpb_reserve_os */
70     0,                          /* rpb_reserve_hw */
71     0,                          /* rpb_checksum */
72     0,                          /* rpb_rxrdy */
73     0,                          /* rpb_txrdy */
74     0,                          /* rpb_dsrdb_off */
75     {0},                        /* rpb_rpb_tbhint */
76 };
77
78 #define ROUNDUP(x)      (((x) + sizeof(u_int64_t) - 1) \
79                          & ~(sizeof(u_int64_t) - 1))
80
81 u_int64_t
82 checksum(void *p, size_t size)
83 {
84     u_int64_t sum = 0;
85     u_int64_t *lp = (u_int64_t *)p;
86     int i;
87
88     printf("checksum(%p, %d)\n", p, size);
89     size = ROUNDUP(size) / sizeof(u_int64_t);
90     for (i = 0; i < size; i++)
91         sum += lp[i];
92 }
93
94 size_t
95 size_mddt()
96 {
97     int count = 0;
98     MEMORY_DESCRIPTOR *desc;
99
100     for (desc = GetMemoryDescriptor(NULL); desc;
101          desc = GetMemoryDescriptor(desc)) {
102         count++;
103     }
104
105     return ROUNDUP(sizeof(struct mddt)
106                    + (count - 1) * sizeof(struct mddt_cluster));
107 }
108
109 void
110 write_mddt(struct mddt *mddt, size_t size)
111 {
112     int count = 0, i;
113     MEMORY_DESCRIPTOR *desc;
114     u_int64_t *p;
115
116     memset(mddt, 0, sizeof(struct mddt));
117     for (desc = GetMemoryDescriptor(NULL); desc;
118          desc = GetMemoryDescriptor(desc)) {
119         struct mddt_cluster *mc;
120         mc = &mddt->mddt_clusters[count];
121         mc->mddt_pfn = desc->BasePage;
122         mc->mddt_pg_cnt = desc->PageCount;
123         mc->mddt_pg_test = 0;
124         mc->mddt_v_bitaddr = 0;
125         mc->mddt_p_bitaddr = 0;
126         mc->mddt_bit_cksum = 0;
127
128         /*
129          * Not sure about the FirmwareTemporary bit but my 164LX has
130          * about 60Mb marked this way.
131          */
132         if (desc->Type == MemoryFree || desc->Type == MemoryFirmwareTemporary)
133             mc->mddt_usage = MDDT_SYSTEM;
134         else if (desc->Type == MemorySpecialMemory)
135             mc->mddt_usage = MDDT_NONVOLATILE; /* ?? */
136         else
137             mc->mddt_usage = MDDT_PALCODE;
138         count++;
139     }
140     mddt->mddt_cluster_cnt = count;
141     mddt->mddt_cksum = checksum(mddt, size);
142 }
143
144 size_t
145 size_rpb()
146 {
147     return sizeof(struct rpb) + size_mddt();
148 }
149
150 void
151 write_rpb(struct rpb *rpb)
152 {
153     EXTENDED_SYSTEM_INFORMATION sysinfo;
154     SYSTEM_ID *sysid;
155
156     ReturnExtendedSystemInformation(&sysinfo);
157
158     memset(rpb, 0, sizeof(struct rpb));
159     rpb->rpb_phys = 0;                  /* XXX */
160     strcpy(rpb->rpb_magic, "HWRPB");
161     rpb->rpb_version = HWRPB_DSRDB_MINVERS;
162     rpb->rpb_size = sizeof(struct rpb);
163     rpb->rpb_primary_cpu_id = 0;        /* XXX */
164     rpb->rpb_page_size = sysinfo.ProcessorPageSize;
165     rpb->rpb_phys_addr_size = sysinfo.NumberOfPhysicalAddressBits;
166     rpb->rpb_max_asn = sysinfo.MaximumAddressSpaceNumber;
167     rpb->rpb_type = ST_EB164;           /* XXX */
168     rpb->rpb_variation = SV_ST_ALPHAPC164LX_533; /* XXX */
169     rpb->rpb_intr_freq = 1024*4096;     /* XXX */
170     rpb->rpb_cc_freq = 533000000;       /* XXX */
171     rpb->rpb_memdat_off = sizeof(struct rpb);
172     write_mddt((struct mddt *)((caddr_t) rpb + rpb->rpb_memdat_off),
173                size_mddt());
174     rpb->rpb_checksum = checksum(rpb, 280); /* only sum first 280 bytes */
175 }
176
177 struct rpb *
178 make_rpb()
179 {
180     EXTENDED_SYSTEM_INFORMATION sysinfo;
181     struct rpb *rpb;
182
183     ReturnExtendedSystemInformation(&sysinfo);
184     printf("sysinfo.ProcessorId = %x\n", sysinfo.ProcessorId);
185     printf("sysinfo.ProcessorRevision = %d\n", sysinfo.ProcessorRevision);
186     printf("sysinfo.ProcessorPageSize = %d\n", sysinfo.ProcessorPageSize);
187     printf("sysinfo.NumberOfPhysicalAddressBits = %d\n", sysinfo.NumberOfPhysicalAddressBits);
188     printf("sysinfo.MaximumAddressSpaceNumber = %d\n", sysinfo.MaximumAddressSpaceNumber);
189     printf("sysinfo.ProcessorCycleCounterPeriod = %d\n", sysinfo.ProcessorCycleCounterPeriod);
190     printf("sysinfo.SystemRevision = %d\n", sysinfo.SystemRevision);
191     printf("sysinfo.SystemSerialNumber = %s\n", sysinfo.SystemSerialNumber);
192     printf("sysinfo.FirmwareVersion = %s\n", sysinfo.FirmwareVersion);
193
194     rpb = malloc(size_rpb());
195     write_rpb(rpb);
196     return rpb;
197 }