clock.c revision 1.56 1 1.56 rin /* $NetBSD: clock.c,v 1.56 2020/05/19 08:43:30 rin Exp $ */
2 1.6 cgd
3 1.1 chopps /*
4 1.52 rmind * Copyright (c) 1988 University of Utah.
5 1.1 chopps * Copyright (c) 1982, 1990 The Regents of the University of California.
6 1.1 chopps * All rights reserved.
7 1.1 chopps *
8 1.1 chopps * This code is derived from software contributed to Berkeley by
9 1.1 chopps * the Systems Programming Group of the University of Utah Computer
10 1.1 chopps * Science Department.
11 1.1 chopps *
12 1.1 chopps * Redistribution and use in source and binary forms, with or without
13 1.1 chopps * modification, are permitted provided that the following conditions
14 1.1 chopps * are met:
15 1.1 chopps * 1. Redistributions of source code must retain the above copyright
16 1.1 chopps * notice, this list of conditions and the following disclaimer.
17 1.1 chopps * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 chopps * notice, this list of conditions and the following disclaimer in the
19 1.1 chopps * documentation and/or other materials provided with the distribution.
20 1.42 agc * 3. Neither the name of the University nor the names of its contributors
21 1.42 agc * may be used to endorse or promote products derived from this software
22 1.42 agc * without specific prior written permission.
23 1.42 agc *
24 1.42 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.42 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.42 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.42 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.42 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.42 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.42 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.42 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.42 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.42 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.42 agc * SUCH DAMAGE.
35 1.42 agc *
36 1.42 agc * from: Utah $Hdr: clock.c 1.18 91/01/21$
37 1.42 agc *
38 1.42 agc * @(#)clock.c 7.6 (Berkeley) 5/7/91
39 1.42 agc */
40 1.38 aymeric
41 1.38 aymeric #include <sys/cdefs.h>
42 1.56 rin __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.56 2020/05/19 08:43:30 rin Exp $");
43 1.1 chopps
44 1.1 chopps #include <sys/param.h>
45 1.1 chopps #include <sys/kernel.h>
46 1.1 chopps #include <sys/device.h>
47 1.13 veego #include <sys/systm.h>
48 1.47 mhitch #include <sys/timetc.h>
49 1.1 chopps #include <machine/psl.h>
50 1.1 chopps #include <machine/cpu.h>
51 1.1 chopps #include <amiga/amiga/device.h>
52 1.1 chopps #include <amiga/amiga/custom.h>
53 1.1 chopps #include <amiga/amiga/cia.h>
54 1.14 is #ifdef DRACO
55 1.14 is #include <amiga/amiga/drcustom.h>
56 1.33 is #include <m68k/include/asm_single.h>
57 1.14 is #endif
58 1.1 chopps #include <amiga/dev/rtc.h>
59 1.8 chopps #include <amiga/dev/zbusvar.h>
60 1.1 chopps
61 1.1 chopps #if defined(PROF) && defined(PROFTIMER)
62 1.1 chopps #include <sys/PROF.h>
63 1.1 chopps #endif
64 1.1 chopps
65 1.1 chopps /*
66 1.1 chopps * Machine-dependent clock routines.
67 1.1 chopps *
68 1.1 chopps * Startrtclock restarts the real-time clock, which provides
69 1.1 chopps * hardclock interrupts to kern_clock.c.
70 1.1 chopps *
71 1.1 chopps * Inittodr initializes the time of day hardware which provides
72 1.1 chopps * date functions.
73 1.1 chopps *
74 1.1 chopps * Resettodr restores the time of day hardware after a time change.
75 1.1 chopps *
76 1.1 chopps * A note on the real-time clock:
77 1.47 mhitch * We actually load the clock with amiga_clk_interval-1 instead of amiga_clk_interval.
78 1.1 chopps * This is because the counter decrements to zero after N+1 enabled clock
79 1.1 chopps * periods where N is the value loaded into the counter.
80 1.1 chopps */
81 1.1 chopps
82 1.53 matt int clockmatch(device_t, cfdata_t, void *);
83 1.53 matt void clockattach(device_t, device_t, void *);
84 1.37 aymeric void cpu_initclocks(void);
85 1.53 matt static void calibrate_delay(device_t);
86 1.51 phx
87 1.51 phx /* the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz.
88 1.51 phx We're using a 100 Hz clock. */
89 1.51 phx int amiga_clk_interval;
90 1.51 phx int eclockfreq;
91 1.51 phx struct CIA *clockcia;
92 1.51 phx
93 1.51 phx static u_int clk_getcounter(struct timecounter *);
94 1.51 phx
95 1.51 phx static struct timecounter clk_timecounter = {
96 1.51 phx clk_getcounter, /* get_timecount */
97 1.51 phx 0, /* no poll_pps */
98 1.51 phx ~0u, /* counter_mask */
99 1.51 phx 0, /* frequency */
100 1.51 phx "clock", /* name, overriden later */
101 1.51 phx 100, /* quality */
102 1.51 phx NULL, /* prev */
103 1.51 phx NULL, /* next */
104 1.51 phx };
105 1.1 chopps
106 1.53 matt CFATTACH_DECL_NEW(clock, 0,
107 1.41 thorpej clockmatch, clockattach, NULL, NULL);
108 1.1 chopps
109 1.1 chopps int
110 1.54 chs clockmatch(device_t parent, cfdata_t cf, void *aux)
111 1.1 chopps {
112 1.54 chs if (matchname("clock", aux))
113 1.1 chopps return(1);
114 1.1 chopps return(0);
115 1.1 chopps }
116 1.1 chopps
117 1.1 chopps /*
118 1.1 chopps * Start the real-time clock.
119 1.1 chopps */
120 1.1 chopps void
121 1.54 chs clockattach(device_t parent, device_t self, void *aux)
122 1.1 chopps {
123 1.43 jmc const char *clockchip;
124 1.1 chopps unsigned short interval;
125 1.51 phx int chipfreq;
126 1.18 is #ifdef DRACO
127 1.18 is u_char dracorev;
128 1.18 is #endif
129 1.1 chopps
130 1.4 chopps if (eclockfreq == 0)
131 1.4 chopps eclockfreq = 715909; /* guess NTSC */
132 1.37 aymeric
133 1.51 phx chipfreq = eclockfreq;
134 1.51 phx
135 1.14 is #ifdef DRACO
136 1.51 phx dracorev = is_draco();
137 1.18 is if (dracorev >= 4) {
138 1.51 phx chipfreq = eclockfreq / 7;
139 1.18 is clockchip = "QuickLogic";
140 1.18 is } else if (dracorev) {
141 1.14 is clockcia = (struct CIA *)CIAAbase;
142 1.18 is clockchip = "CIA A";
143 1.37 aymeric } else
144 1.14 is #endif
145 1.14 is {
146 1.14 is clockcia = (struct CIA *)CIABbase;
147 1.18 is clockchip = "CIA B";
148 1.14 is }
149 1.14 is
150 1.56 rin /* round nearest to mitigate clock drift for PAL */
151 1.51 phx amiga_clk_interval = chipfreq / hz;
152 1.56 rin if (chipfreq % hz >= hz / 2)
153 1.56 rin amiga_clk_interval++;
154 1.47 mhitch
155 1.54 chs if (self != NULL) { /* real autoconfig? */
156 1.23 is printf(": %s system hz %d hardware hz %d\n", clockchip, hz,
157 1.51 phx chipfreq);
158 1.51 phx
159 1.51 phx clk_timecounter.tc_name = clockchip;
160 1.51 phx clk_timecounter.tc_frequency = chipfreq;
161 1.47 mhitch tc_init(&clk_timecounter);
162 1.47 mhitch }
163 1.18 is
164 1.18 is #ifdef DRACO
165 1.18 is if (dracorev >= 4) {
166 1.37 aymeric /*
167 1.18 is * can't preload anything beforehand, timer is free_running;
168 1.18 is * but need this for delay calibration.
169 1.18 is */
170 1.18 is
171 1.47 mhitch draco_ioct->io_timerlo = amiga_clk_interval & 0xff;
172 1.47 mhitch draco_ioct->io_timerhi = amiga_clk_interval >> 8;
173 1.18 is
174 1.54 chs calibrate_delay(self);
175 1.51 phx
176 1.18 is return;
177 1.18 is }
178 1.18 is #endif
179 1.1 chopps /*
180 1.37 aymeric * stop timer A
181 1.1 chopps */
182 1.14 is clockcia->cra = clockcia->cra & 0xc0;
183 1.14 is clockcia->icr = 1 << 0; /* disable timer A interrupt */
184 1.14 is interval = clockcia->icr; /* and make sure it's clear */
185 1.1 chopps
186 1.1 chopps /*
187 1.1 chopps * load interval into registers.
188 1.1 chopps * the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz
189 1.1 chopps */
190 1.47 mhitch interval = amiga_clk_interval - 1;
191 1.1 chopps
192 1.1 chopps /*
193 1.1 chopps * order of setting is important !
194 1.1 chopps */
195 1.14 is clockcia->talo = interval & 0xff;
196 1.14 is clockcia->tahi = interval >> 8;
197 1.18 is /*
198 1.18 is * start timer A in continuous mode
199 1.18 is */
200 1.18 is clockcia->cra = (clockcia->cra & 0xc0) | 1;
201 1.51 phx
202 1.54 chs calibrate_delay(self);
203 1.1 chopps }
204 1.1 chopps
205 1.1 chopps void
206 1.37 aymeric cpu_initclocks(void)
207 1.1 chopps {
208 1.20 mhitch #ifdef DRACO
209 1.18 is unsigned char dracorev;
210 1.18 is dracorev = is_draco();
211 1.18 is if (dracorev >= 4) {
212 1.47 mhitch draco_ioct->io_timerlo = amiga_clk_interval & 0xFF;
213 1.47 mhitch draco_ioct->io_timerhi = amiga_clk_interval >> 8;
214 1.18 is draco_ioct->io_timerrst = 0; /* any value resets */
215 1.33 is single_inst_bset_b(draco_ioct->io_status2, DRSTAT2_TMRINTENA);
216 1.18 is
217 1.18 is return;
218 1.18 is }
219 1.18 is #endif
220 1.1 chopps /*
221 1.1 chopps * enable interrupts for timer A
222 1.1 chopps */
223 1.14 is clockcia->icr = (1<<7) | (1<<0);
224 1.1 chopps
225 1.1 chopps /*
226 1.1 chopps * start timer A in continuous shot mode
227 1.1 chopps */
228 1.14 is clockcia->cra = (clockcia->cra & 0xc0) | 1;
229 1.37 aymeric
230 1.1 chopps /*
231 1.1 chopps * and globally enable interrupts for ciab
232 1.1 chopps */
233 1.14 is #ifdef DRACO
234 1.18 is if (dracorev) /* we use cia a on DraCo */
235 1.33 is single_inst_bset_b(*draco_intena, DRIRQ_INT2);
236 1.14 is else
237 1.14 is #endif
238 1.14 is custom.intena = INTF_SETCLR | INTF_EXTER;
239 1.18 is
240 1.1 chopps }
241 1.1 chopps
242 1.13 veego void
243 1.43 jmc setstatclockrate(int hertz)
244 1.1 chopps {
245 1.1 chopps }
246 1.1 chopps
247 1.1 chopps /*
248 1.51 phx * Returns ticks since last recorded clock "tick"
249 1.1 chopps * (i.e. clock interrupt).
250 1.1 chopps */
251 1.47 mhitch static u_int
252 1.47 mhitch clk_gettick(void)
253 1.1 chopps {
254 1.18 is u_int interval;
255 1.1 chopps u_char hi, hi2, lo;
256 1.1 chopps
257 1.14 is #ifdef DRACO
258 1.18 is if (is_draco() >= 4) {
259 1.18 is hi2 = draco_ioct->io_chiprev; /* latch timer */
260 1.18 is hi = draco_ioct->io_timerhi;
261 1.18 is lo = draco_ioct->io_timerlo;
262 1.18 is interval = ((hi<<8) | lo);
263 1.47 mhitch if (interval > amiga_clk_interval) /* timer underflow */
264 1.47 mhitch interval = 65536 + amiga_clk_interval - interval;
265 1.18 is else
266 1.47 mhitch interval = amiga_clk_interval - interval;
267 1.1 chopps
268 1.18 is } else
269 1.14 is #endif
270 1.18 is {
271 1.18 is hi = clockcia->tahi;
272 1.18 is lo = clockcia->talo;
273 1.18 is hi2 = clockcia->tahi;
274 1.18 is if (hi != hi2) {
275 1.18 is lo = clockcia->talo;
276 1.18 is hi = hi2;
277 1.18 is }
278 1.1 chopps
279 1.47 mhitch interval = (amiga_clk_interval - 1) - ((hi<<8) | lo);
280 1.37 aymeric
281 1.1 chopps /*
282 1.18 is * should read ICR and if there's an int pending, adjust
283 1.18 is * interval. However, since reading ICR clears the interrupt,
284 1.18 is * we'd lose a hardclock int, and this is not tolerable.
285 1.1 chopps */
286 1.1 chopps }
287 1.1 chopps
288 1.47 mhitch return interval;
289 1.47 mhitch }
290 1.47 mhitch
291 1.47 mhitch static u_int
292 1.47 mhitch clk_getcounter(struct timecounter *tc)
293 1.47 mhitch {
294 1.51 phx static int prev_hardclock;
295 1.51 phx static u_int prev_counter;
296 1.51 phx int cur_hardclock;
297 1.51 phx u_int counter;
298 1.47 mhitch
299 1.47 mhitch do {
300 1.51 phx cur_hardclock = hardclock_ticks;
301 1.51 phx counter = clk_gettick();
302 1.51 phx } while (cur_hardclock != hardclock_ticks);
303 1.47 mhitch
304 1.50 phx /*
305 1.50 phx * Handle the situation of a wrapped interval counter, while
306 1.50 phx * the hardclock() interrupt was not yet executed to update
307 1.50 phx * hardclock_ticks.
308 1.50 phx */
309 1.51 phx if (cur_hardclock < prev_hardclock)
310 1.51 phx cur_hardclock = prev_hardclock;
311 1.51 phx if (counter < prev_counter && cur_hardclock == prev_hardclock)
312 1.51 phx cur_hardclock++;
313 1.51 phx
314 1.51 phx prev_hardclock = cur_hardclock;
315 1.51 phx prev_counter = counter;
316 1.51 phx
317 1.51 phx return cur_hardclock * amiga_clk_interval + counter;
318 1.51 phx }
319 1.51 phx
320 1.51 phx /*
321 1.51 phx * Calibrate delay loop.
322 1.51 phx * We use two iterations because we don't have enough bits to do a factor of
323 1.51 phx * 8 with better than 1%.
324 1.51 phx *
325 1.51 phx * XXX Note that we MUST stay below 1 tick if using clk_gettick(), even for
326 1.51 phx * underestimated values of delaydivisor.
327 1.51 phx *
328 1.51 phx * XXX the "ns" below is only correct for a shift of 10 bits, and even then
329 1.51 phx * off by 2.4%
330 1.51 phx */
331 1.51 phx static void
332 1.54 chs calibrate_delay(device_t self)
333 1.51 phx {
334 1.51 phx unsigned long t1, t2;
335 1.51 phx extern u_int32_t delaydivisor;
336 1.51 phx /* XXX this should be defined elsewhere */
337 1.50 phx
338 1.54 chs if (self)
339 1.51 phx printf("Calibrating delay loop... ");
340 1.51 phx
341 1.51 phx do {
342 1.51 phx t1 = clk_gettick();
343 1.51 phx delay(1024);
344 1.51 phx t2 = clk_gettick();
345 1.51 phx } while (t2 <= t1);
346 1.51 phx t2 = ((t2 - t1) * 1000000) / (amiga_clk_interval * hz);
347 1.51 phx delaydivisor = (delaydivisor * t2 + 1023) >> 10;
348 1.51 phx #ifdef DEBUG
349 1.54 chs if (self)
350 1.51 phx printf("\ndiff %ld us, new divisor %u/1024 us\n", t2,
351 1.51 phx delaydivisor);
352 1.51 phx do {
353 1.51 phx t1 = clk_gettick();
354 1.51 phx delay(1024);
355 1.51 phx t2 = clk_gettick();
356 1.51 phx } while (t2 <= t1);
357 1.51 phx t2 = ((t2 - t1) * 1000000) / (amiga_clk_interval * hz);
358 1.51 phx delaydivisor = (delaydivisor * t2 + 1023) >> 10;
359 1.54 chs if (self)
360 1.51 phx printf("diff %ld us, new divisor %u/1024 us\n", t2,
361 1.51 phx delaydivisor);
362 1.51 phx #endif
363 1.51 phx do {
364 1.51 phx t1 = clk_gettick();
365 1.51 phx delay(1024);
366 1.51 phx t2 = clk_gettick();
367 1.51 phx } while (t2 <= t1);
368 1.51 phx t2 = ((t2 - t1) * 1000000) / (amiga_clk_interval * hz);
369 1.51 phx delaydivisor = (delaydivisor * t2 + 1023) >> 10;
370 1.51 phx #ifdef DEBUG
371 1.54 chs if (self)
372 1.51 phx printf("diff %ld us, new divisor ", t2);
373 1.51 phx #endif
374 1.54 chs if (self)
375 1.51 phx printf("%u/1024 us\n", delaydivisor);
376 1.1 chopps }
377 1.1 chopps
378 1.1 chopps #if notyet
379 1.1 chopps
380 1.1 chopps /* implement this later. I'd suggest using both timers in CIA-A, they're
381 1.1 chopps not yet used. */
382 1.1 chopps
383 1.1 chopps #include "clock.h"
384 1.1 chopps #if NCLOCK > 0
385 1.1 chopps /*
386 1.1 chopps * /dev/clock: mappable high resolution timer.
387 1.1 chopps *
388 1.1 chopps * This code implements a 32-bit recycling counter (with a 4 usec period)
389 1.1 chopps * using timers 2 & 3 on the 6840 clock chip. The counter can be mapped
390 1.1 chopps * RO into a user's address space to achieve low overhead (no system calls),
391 1.1 chopps * high-precision timing.
392 1.1 chopps *
393 1.1 chopps * Note that timer 3 is also used for the high precision profiling timer
394 1.1 chopps * (PROFTIMER code above). Care should be taken when both uses are
395 1.1 chopps * configured as only a token effort is made to avoid conflicting use.
396 1.1 chopps */
397 1.1 chopps #include <sys/proc.h>
398 1.1 chopps #include <sys/resourcevar.h>
399 1.1 chopps #include <sys/ioctl.h>
400 1.1 chopps #include <sys/malloc.h>
401 1.35 mrg #include <uvm/uvm_extern.h>
402 1.1 chopps #include <amiga/amiga/clockioctl.h>
403 1.1 chopps #include <sys/specdev.h>
404 1.1 chopps #include <sys/vnode.h>
405 1.1 chopps #include <sys/mman.h>
406 1.1 chopps
407 1.1 chopps int clockon = 0; /* non-zero if high-res timer enabled */
408 1.1 chopps #ifdef PROFTIMER
409 1.1 chopps int profprocs = 0; /* # of procs using profiling timer */
410 1.1 chopps #endif
411 1.1 chopps #ifdef DEBUG
412 1.1 chopps int clockdebug = 0;
413 1.1 chopps #endif
414 1.1 chopps
415 1.1 chopps /*ARGSUSED*/
416 1.37 aymeric int
417 1.37 aymeric clockopen(dev_t dev, int flags)
418 1.1 chopps {
419 1.1 chopps #ifdef PROFTIMER
420 1.1 chopps #ifdef PROF
421 1.1 chopps /*
422 1.1 chopps * Kernel profiling enabled, give up.
423 1.1 chopps */
424 1.1 chopps if (profiling)
425 1.1 chopps return(EBUSY);
426 1.1 chopps #endif
427 1.1 chopps /*
428 1.1 chopps * If any user processes are profiling, give up.
429 1.1 chopps */
430 1.1 chopps if (profprocs)
431 1.1 chopps return(EBUSY);
432 1.1 chopps #endif
433 1.1 chopps if (!clockon) {
434 1.1 chopps startclock();
435 1.1 chopps clockon++;
436 1.1 chopps }
437 1.1 chopps return(0);
438 1.1 chopps }
439 1.1 chopps
440 1.1 chopps /*ARGSUSED*/
441 1.37 aymeric int
442 1.37 aymeric clockclose(dev_t dev, int flags)
443 1.1 chopps {
444 1.46 christos (void) clockunmmap(dev, (void *)0, curproc); /* XXX */
445 1.1 chopps stopclock();
446 1.1 chopps clockon = 0;
447 1.1 chopps return(0);
448 1.1 chopps }
449 1.1 chopps
450 1.1 chopps /*ARGSUSED*/
451 1.37 aymeric int
452 1.46 christos clockioctl(dev_t dev, u_long cmd, void *data, int flag, struct proc *p)
453 1.1 chopps {
454 1.1 chopps int error = 0;
455 1.37 aymeric
456 1.1 chopps switch (cmd) {
457 1.1 chopps
458 1.1 chopps case CLOCKMAP:
459 1.46 christos error = clockmmap(dev, (void **)data, p);
460 1.1 chopps break;
461 1.1 chopps
462 1.1 chopps case CLOCKUNMAP:
463 1.46 christos error = clockunmmap(dev, *(void **)data, p);
464 1.1 chopps break;
465 1.1 chopps
466 1.1 chopps case CLOCKGETRES:
467 1.1 chopps *(int *)data = CLK_RESOLUTION;
468 1.1 chopps break;
469 1.1 chopps
470 1.1 chopps default:
471 1.1 chopps error = EINVAL;
472 1.1 chopps break;
473 1.1 chopps }
474 1.1 chopps return(error);
475 1.1 chopps }
476 1.1 chopps
477 1.1 chopps /*ARGSUSED*/
478 1.37 aymeric void
479 1.37 aymeric clockmap(dev_t dev, int off, int prot)
480 1.1 chopps {
481 1.55 phx return MD_BTOP(off + (INTIOBASE+CLKBASE+CLKSR-1));
482 1.1 chopps }
483 1.1 chopps
484 1.37 aymeric int
485 1.46 christos clockmmap(dev_t dev, void **addrp, struct proc *p)
486 1.1 chopps {
487 1.1 chopps int error;
488 1.1 chopps struct vnode vn;
489 1.1 chopps struct specinfo si;
490 1.1 chopps int flags;
491 1.1 chopps
492 1.1 chopps flags = MAP_FILE|MAP_SHARED;
493 1.1 chopps if (*addrp)
494 1.1 chopps flags |= MAP_FIXED;
495 1.1 chopps else
496 1.46 christos *addrp = (void *)0x1000000; /* XXX */
497 1.1 chopps vn.v_type = VCHR; /* XXX */
498 1.1 chopps vn.v_specinfo = &si; /* XXX */
499 1.1 chopps vn.v_rdev = dev; /* XXX */
500 1.1 chopps error = vm_mmap(&p->p_vmspace->vm_map, (vm_offset_t *)addrp,
501 1.46 christos PAGE_SIZE, VM_PROT_ALL, flags, (void *)&vn, 0);
502 1.1 chopps return(error);
503 1.1 chopps }
504 1.1 chopps
505 1.37 aymeric int
506 1.46 christos clockunmmap(dev_t dev, void *addr, struct proc *p)
507 1.1 chopps {
508 1.1 chopps int rv;
509 1.1 chopps
510 1.1 chopps if (addr == 0)
511 1.1 chopps return(EINVAL); /* XXX: how do we deal with this? */
512 1.36 chs uvm_deallocate(p->p_vmspace->vm_map, (vm_offset_t)addr, PAGE_SIZE);
513 1.36 chs return 0;
514 1.1 chopps }
515 1.1 chopps
516 1.37 aymeric void
517 1.37 aymeric startclock(void)
518 1.1 chopps {
519 1.1 chopps register struct clkreg *clk = (struct clkreg *)clkstd[0];
520 1.1 chopps
521 1.1 chopps clk->clk_msb2 = -1; clk->clk_lsb2 = -1;
522 1.1 chopps clk->clk_msb3 = -1; clk->clk_lsb3 = -1;
523 1.1 chopps
524 1.1 chopps clk->clk_cr2 = CLK_CR3;
525 1.1 chopps clk->clk_cr3 = CLK_OENAB|CLK_8BIT;
526 1.1 chopps clk->clk_cr2 = CLK_CR1;
527 1.1 chopps clk->clk_cr1 = CLK_IENAB;
528 1.1 chopps }
529 1.1 chopps
530 1.37 aymeric void
531 1.37 aymeric stopclock(void)
532 1.1 chopps {
533 1.1 chopps register struct clkreg *clk = (struct clkreg *)clkstd[0];
534 1.1 chopps
535 1.1 chopps clk->clk_cr2 = CLK_CR3;
536 1.1 chopps clk->clk_cr3 = 0;
537 1.1 chopps clk->clk_cr2 = CLK_CR1;
538 1.1 chopps clk->clk_cr1 = CLK_IENAB;
539 1.1 chopps }
540 1.1 chopps #endif
541 1.1 chopps
542 1.1 chopps #endif
543 1.1 chopps
544 1.1 chopps
545 1.1 chopps #ifdef PROFTIMER
546 1.1 chopps /*
547 1.1 chopps * This code allows the amiga kernel to use one of the extra timers on
548 1.1 chopps * the clock chip for profiling, instead of the regular system timer.
549 1.1 chopps * The advantage of this is that the profiling timer can be turned up to
550 1.1 chopps * a higher interrupt rate, giving finer resolution timing. The profclock
551 1.1 chopps * routine is called from the lev6intr in locore, and is a specialized
552 1.1 chopps * routine that calls addupc. The overhead then is far less than if
553 1.1 chopps * hardclock/softclock was called. Further, the context switch code in
554 1.1 chopps * locore has been changed to turn the profile clock on/off when switching
555 1.1 chopps * into/out of a process that is profiling (startprofclock/stopprofclock).
556 1.1 chopps * This reduces the impact of the profiling clock on other users, and might
557 1.37 aymeric * possibly increase the accuracy of the profiling.
558 1.1 chopps */
559 1.1 chopps int profint = PRF_INTERVAL; /* Clock ticks between interrupts */
560 1.1 chopps int profscale = 0; /* Scale factor from sys clock to prof clock */
561 1.1 chopps char profon = 0; /* Is profiling clock on? */
562 1.1 chopps
563 1.1 chopps /* profon values - do not change, locore.s assumes these values */
564 1.1 chopps #define PRF_NONE 0x00
565 1.1 chopps #define PRF_USER 0x01
566 1.1 chopps #define PRF_KERNEL 0x80
567 1.1 chopps
568 1.37 aymeric void
569 1.37 aymeric initprofclock(void)
570 1.1 chopps {
571 1.1 chopps #if NCLOCK > 0
572 1.1 chopps struct proc *p = curproc; /* XXX */
573 1.1 chopps
574 1.1 chopps /*
575 1.1 chopps * If the high-res timer is running, force profiling off.
576 1.1 chopps * Unfortunately, this gets reflected back to the user not as
577 1.1 chopps * an error but as a lack of results.
578 1.1 chopps */
579 1.1 chopps if (clockon) {
580 1.1 chopps p->p_stats->p_prof.pr_scale = 0;
581 1.1 chopps return;
582 1.1 chopps }
583 1.1 chopps /*
584 1.1 chopps * Keep track of the number of user processes that are profiling
585 1.1 chopps * by checking the scale value.
586 1.1 chopps *
587 1.1 chopps * XXX: this all assumes that the profiling code is well behaved;
588 1.1 chopps * i.e. profil() is called once per process with pcscale non-zero
589 1.1 chopps * to turn it on, and once with pcscale zero to turn it off.
590 1.1 chopps * Also assumes you don't do any forks or execs. Oh well, there
591 1.1 chopps * is always adb...
592 1.1 chopps */
593 1.1 chopps if (p->p_stats->p_prof.pr_scale)
594 1.1 chopps profprocs++;
595 1.1 chopps else
596 1.1 chopps profprocs--;
597 1.1 chopps #endif
598 1.1 chopps /*
599 1.1 chopps * The profile interrupt interval must be an even divisor
600 1.47 mhitch * of the amiga_clk_interval so that scaling from a system clock
601 1.1 chopps * tick to a profile clock tick is possible using integer math.
602 1.1 chopps */
603 1.47 mhitch if (profint > amiga_clk_interval || (amiga_clk_interval % profint) != 0)
604 1.47 mhitch profint = amiga_clk_interval;
605 1.47 mhitch profscale = amiga_clk_interval / profint;
606 1.1 chopps }
607 1.1 chopps
608 1.37 aymeric void
609 1.37 aymeric startprofclock(void)
610 1.1 chopps {
611 1.1 chopps unsigned short interval;
612 1.1 chopps
613 1.1 chopps /* stop timer B */
614 1.14 is clockcia->crb = clockcia->crb & 0xc0;
615 1.1 chopps
616 1.1 chopps /* load interval into registers.
617 1.1 chopps the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz */
618 1.1 chopps
619 1.1 chopps interval = profint - 1;
620 1.1 chopps
621 1.1 chopps /* order of setting is important ! */
622 1.14 is clockcia->tblo = interval & 0xff;
623 1.14 is clockcia->tbhi = interval >> 8;
624 1.1 chopps
625 1.1 chopps /* enable interrupts for timer B */
626 1.14 is clockcia->icr = (1<<7) | (1<<1);
627 1.1 chopps
628 1.1 chopps /* start timer B in continuous shot mode */
629 1.14 is clockcia->crb = (clockcia->crb & 0xc0) | 1;
630 1.1 chopps }
631 1.1 chopps
632 1.37 aymeric void
633 1.37 aymeric stopprofclock(void)
634 1.1 chopps {
635 1.1 chopps /* stop timer B */
636 1.14 is clockcia->crb = clockcia->crb & 0xc0;
637 1.1 chopps }
638 1.1 chopps
639 1.1 chopps #ifdef PROF
640 1.1 chopps /*
641 1.1 chopps * profclock() is expanded in line in lev6intr() unless profiling kernel.
642 1.1 chopps * Assumes it is called with clock interrupts blocked.
643 1.1 chopps */
644 1.37 aymeric void
645 1.46 christos profclock(void *pc, int ps)
646 1.1 chopps {
647 1.1 chopps /*
648 1.1 chopps * Came from user mode.
649 1.1 chopps * If this process is being profiled record the tick.
650 1.1 chopps */
651 1.1 chopps if (USERMODE(ps)) {
652 1.1 chopps if (p->p_stats.p_prof.pr_scale)
653 1.1 chopps addupc(pc, &curproc->p_stats.p_prof, 1);
654 1.1 chopps }
655 1.1 chopps /*
656 1.1 chopps * Came from kernel (supervisor) mode.
657 1.1 chopps * If we are profiling the kernel, record the tick.
658 1.1 chopps */
659 1.1 chopps else if (profiling < 2) {
660 1.1 chopps register int s = pc - s_lowpc;
661 1.1 chopps
662 1.1 chopps if (s < s_textsize)
663 1.1 chopps kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
664 1.1 chopps }
665 1.1 chopps /*
666 1.1 chopps * Kernel profiling was on but has been disabled.
667 1.1 chopps * Mark as no longer profiling kernel and if all profiling done,
668 1.1 chopps * disable the clock.
669 1.1 chopps */
670 1.1 chopps if (profiling && (profon & PRF_KERNEL)) {
671 1.1 chopps profon &= ~PRF_KERNEL;
672 1.1 chopps if (profon == PRF_NONE)
673 1.1 chopps stopprofclock();
674 1.1 chopps }
675 1.1 chopps }
676 1.1 chopps #endif
677 1.1 chopps #endif
678