clock.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*-
  2. * SPDX-License-Identifier: BSD-4-Clause AND BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (C) 1995, 1996 Wolfgang Solfrank.
  5. * Copyright (C) 1995, 1996 TooLs GmbH.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. All advertising materials mentioning features or use of this software
  17. * must display the following acknowledgement:
  18. * This product includes software developed by TooLs GmbH.
  19. * 4. The name of TooLs GmbH may not be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
  23. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  24. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  28. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  31. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. * $NetBSD: clock.c,v 1.9 2000/01/19 02:52:19 msaitoh Exp $
  34. */
  35. /*
  36. * Copyright (C) 2001 Benno Rice.
  37. * All rights reserved.
  38. *
  39. * Redistribution and use in source and binary forms, with or without
  40. * modification, are permitted provided that the following conditions
  41. * are met:
  42. * 1. Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * 2. Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in the
  46. * documentation and/or other materials provided with the distribution.
  47. *
  48. * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
  49. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  50. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  51. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  52. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  53. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  54. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  55. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  56. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  57. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  58. */
  59. #include <sys/cdefs.h>
  60. __FBSDID("$FreeBSD$");
  61. #include <sys/param.h>
  62. #include <sys/systm.h>
  63. #include <sys/kernel.h>
  64. #include <sys/bus.h>
  65. #include <sys/interrupt.h>
  66. #include <sys/pcpu.h>
  67. #include <sys/sysctl.h>
  68. #include <sys/timeet.h>
  69. #include <sys/timetc.h>
  70. #include <sys/vdso.h>
  71. #include <dev/ofw/openfirm.h>
  72. #include <machine/clock.h>
  73. #include <machine/cpu.h>
  74. #include <machine/intr_machdep.h>
  75. #include <machine/md_var.h>
  76. #include <machine/smp.h>
  77. /*
  78. * Initially we assume a processor with a bus frequency of 12.5 MHz.
  79. */
  80. static int initialized = 0;
  81. static uint64_t ps_per_tick = 80000;
  82. static u_long ticks_per_sec = 12500000;
  83. static u_long *decr_counts[MAXCPU];
  84. static int decr_et_start(struct eventtimer *et,
  85. sbintime_t first, sbintime_t period);
  86. static int decr_et_stop(struct eventtimer *et);
  87. static timecounter_get_t decr_get_timecount;
  88. static uint32_t decr_vdso_timehands(struct vdso_timehands *vdso_th,
  89. struct timecounter *tc);
  90. #ifdef COMPAT_FREEBSD32
  91. static uint32_t decr_vdso_timehands32(struct vdso_timehands32 *vdso_th32,
  92. struct timecounter *tc);
  93. #endif
  94. struct decr_state {
  95. int mode; /* 0 - off, 1 - periodic, 2 - one-shot. */
  96. int32_t div; /* Periodic divisor. */
  97. };
  98. DPCPU_DEFINE_STATIC(struct decr_state, decr_state);
  99. static struct eventtimer decr_et;
  100. static struct timecounter decr_tc = {
  101. .tc_get_timecount = decr_get_timecount,
  102. .tc_counter_mask = ~0u,
  103. .tc_name = "timebase",
  104. .tc_quality = 1000,
  105. .tc_fill_vdso_timehands = decr_vdso_timehands,
  106. #ifdef COMPAT_FREEBSD32
  107. .tc_fill_vdso_timehands32 = decr_vdso_timehands32,
  108. #endif
  109. };
  110. /*
  111. * Decrementer interrupt handler.
  112. */
  113. void
  114. decr_intr(struct trapframe *frame)
  115. {
  116. struct decr_state *s = DPCPU_PTR(decr_state);
  117. int nticks = 0;
  118. int32_t val;
  119. if (!initialized)
  120. return;
  121. (*decr_counts[curcpu])++;
  122. #ifdef BOOKE
  123. /*
  124. * Interrupt handler must reset DIS to avoid getting another
  125. * interrupt once EE is enabled.
  126. */
  127. mtspr(SPR_TSR, TSR_DIS);
  128. #endif
  129. if (s->mode == 1) {
  130. /*
  131. * Based on the actual time delay since the last decrementer
  132. * reload, we arrange for earlier interrupt next time.
  133. */
  134. __asm ("mfdec %0" : "=r"(val));
  135. while (val < 0) {
  136. val += s->div;
  137. nticks++;
  138. }
  139. mtdec(val);
  140. } else if (s->mode == 2) {
  141. nticks = 1;
  142. decr_et_stop(NULL);
  143. } else if (s->mode == 0) {
  144. /* Potemkin timer ran out without an event. Just reset it. */
  145. decr_et_stop(NULL);
  146. }
  147. while (nticks-- > 0) {
  148. if (decr_et.et_active)
  149. decr_et.et_event_cb(&decr_et, decr_et.et_arg);
  150. }
  151. }
  152. void
  153. cpu_initclocks(void)
  154. {
  155. decr_tc_init();
  156. cpu_initclocks_bsp();
  157. }
  158. /*
  159. * BSP early initialization.
  160. */
  161. void
  162. decr_init(void)
  163. {
  164. struct cpuref cpu;
  165. char buf[32];
  166. /*
  167. * Check the BSP's timebase frequency. Sometimes we can't find the BSP,
  168. * so fall back to the first CPU in this case.
  169. */
  170. if (platform_smp_get_bsp(&cpu) != 0)
  171. platform_smp_first_cpu(&cpu);
  172. ticks_per_sec = platform_timebase_freq(&cpu);
  173. ps_per_tick = 1000000000000 / ticks_per_sec;
  174. set_cputicker(mftb, ticks_per_sec, 0);
  175. snprintf(buf, sizeof(buf), "cpu%d:decrementer", curcpu);
  176. intrcnt_add(buf, &decr_counts[curcpu]);
  177. decr_et_stop(NULL);
  178. initialized = 1;
  179. }
  180. #ifdef SMP
  181. /*
  182. * AP early initialization.
  183. */
  184. void
  185. decr_ap_init(void)
  186. {
  187. char buf[32];
  188. snprintf(buf, sizeof(buf), "cpu%d:decrementer", curcpu);
  189. intrcnt_add(buf, &decr_counts[curcpu]);
  190. decr_et_stop(NULL);
  191. }
  192. #endif
  193. /*
  194. * Final initialization.
  195. */
  196. void
  197. decr_tc_init(void)
  198. {
  199. decr_tc.tc_frequency = ticks_per_sec;
  200. tc_init(&decr_tc);
  201. decr_et.et_name = "decrementer";
  202. decr_et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT |
  203. ET_FLAGS_PERCPU;
  204. decr_et.et_quality = 1000;
  205. decr_et.et_frequency = ticks_per_sec;
  206. decr_et.et_min_period = (0x00000002LLU << 32) / ticks_per_sec;
  207. decr_et.et_max_period = (0x7fffffffLLU << 32) / ticks_per_sec;
  208. decr_et.et_start = decr_et_start;
  209. decr_et.et_stop = decr_et_stop;
  210. decr_et.et_priv = NULL;
  211. et_register(&decr_et);
  212. }
  213. uint32_t
  214. decr_vdso_timehands(struct vdso_timehands *vdso_th, struct timecounter *tc)
  215. {
  216. vdso_th->th_algo = VDSO_TH_ALGO_PPC_TB;
  217. bzero(vdso_th->th_res, sizeof(vdso_th->th_res));
  218. return (initialized == 1);
  219. }
  220. #ifdef COMPAT_FREEBSD32
  221. uint32_t
  222. decr_vdso_timehands32(struct vdso_timehands32 *vdso_th32,
  223. struct timecounter *tc)
  224. {
  225. vdso_th32->th_algo = VDSO_TH_ALGO_PPC_TB;
  226. bzero(vdso_th32->th_res, sizeof(vdso_th32->th_res));
  227. return (initialized == 1);
  228. }
  229. #endif
  230. /*
  231. * Event timer start method.
  232. */
  233. static int
  234. decr_et_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
  235. {
  236. struct decr_state *s = DPCPU_PTR(decr_state);
  237. uint32_t fdiv;
  238. #ifdef BOOKE
  239. uint32_t tcr;
  240. #endif
  241. if (period != 0) {
  242. s->mode = 1;
  243. s->div = (decr_et.et_frequency * period) >> 32;
  244. } else {
  245. s->mode = 2;
  246. s->div = 0;
  247. }
  248. if (first != 0)
  249. fdiv = (decr_et.et_frequency * first) >> 32;
  250. else
  251. fdiv = s->div;
  252. #ifdef BOOKE
  253. tcr = mfspr(SPR_TCR);
  254. tcr |= TCR_DIE;
  255. if (s->mode == 1) {
  256. mtspr(SPR_DECAR, s->div);
  257. tcr |= TCR_ARE;
  258. } else
  259. tcr &= ~TCR_ARE;
  260. mtdec(fdiv);
  261. mtspr(SPR_TCR, tcr);
  262. #else
  263. mtdec(fdiv);
  264. #endif
  265. return (0);
  266. }
  267. /*
  268. * Event timer stop method.
  269. */
  270. static int
  271. decr_et_stop(struct eventtimer *et)
  272. {
  273. struct decr_state *s = DPCPU_PTR(decr_state);
  274. #ifdef BOOKE
  275. uint32_t tcr;
  276. #endif
  277. s->mode = 0;
  278. s->div = 0x7fffffff;
  279. #ifdef BOOKE
  280. tcr = mfspr(SPR_TCR);
  281. tcr &= ~(TCR_DIE | TCR_ARE);
  282. mtspr(SPR_TCR, tcr);
  283. #else
  284. mtdec(s->div);
  285. #endif
  286. return (0);
  287. }
  288. /*
  289. * Timecounter get method.
  290. */
  291. static unsigned
  292. decr_get_timecount(struct timecounter *tc)
  293. {
  294. return (mftb());
  295. }
  296. /*
  297. * Wait for about n microseconds (at least!).
  298. */
  299. void
  300. DELAY(int n)
  301. {
  302. volatile u_quad_t tb;
  303. u_quad_t ttb;
  304. TSENTER();
  305. tb = mftb();
  306. ttb = tb + howmany((uint64_t)n * 1000000, ps_per_tick);
  307. nop_prio_vlow();
  308. while (tb < ttb)
  309. tb = mftb();
  310. nop_prio_medium();
  311. TSEXIT();
  312. }