blob: cef3d5723418c7a22f3741e52ad07ec90ceccb2b [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Rudolphbd4bcab2019-06-30 22:12:15 +02002
3#include <types.h>
4#include <symbols.h>
5#include <device/mmio.h>
6#include <ramdetect.h>
7#include <console/console.h>
8
9#define OVERLAP(a, b, s, e) ((b) > (s) && (a) < (e))
10
Asami Doi06993ee2019-08-07 13:40:53 +090011int __weak probe_mb(const uintptr_t dram_start, const uintptr_t size)
Patrick Rudolphbd4bcab2019-06-30 22:12:15 +020012{
13 uintptr_t addr = dram_start + (size * MiB) - sizeof(uint32_t);
Philipp Hug8e365392024-03-01 10:59:56 +000014 static const uint32_t patterns[] = {0x55aa55aa, 0x12345678};
15 void *ptr = (void *)addr;
Patrick Rudolphbd4bcab2019-06-30 22:12:15 +020016 size_t i;
17
Martin Roth3e25f852023-09-04 15:37:07 -060018 /* Don't accidentally clobber oneself. */
Philipp Hug8e365392024-03-01 10:59:56 +000019 if (OVERLAP(addr, addr + sizeof(uint32_t), (uintptr_t)_program, (uintptr_t)_eprogram))
Patrick Rudolphbd4bcab2019-06-30 22:12:15 +020020 return 1;
21
22 uint32_t old = read32(ptr);
23 for (i = 0; i < ARRAY_SIZE(patterns); i++) {
24 write32(ptr, patterns[i]);
25 if (read32(ptr) != patterns[i])
26 break;
27 }
28
29 write32(ptr, old);
30 return i == ARRAY_SIZE(patterns);
31}
32
33/* - 20 as probe_size is in MiB, - 1 as i is signed */
34#define MAX_ADDRESSABLE_SPACE (sizeof(size_t) * 8 - 20 - 1)
35
36/* Probe an area if it's read/writable. */
37size_t probe_ramsize(const uintptr_t dram_start, const size_t probe_size)
38{
39 ssize_t i;
40 size_t msb = 0;
41 size_t discovered = 0;
42
43 static size_t saved_result;
44 if (saved_result)
45 return saved_result;
46
47 /* Find the MSB + 1. */
48 size_t tmp = probe_size;
49 do {
50 msb++;
51 } while (tmp >>= 1);
52
53 /* Limit search to accessible address space */
54 msb = MIN(msb, MAX_ADDRESSABLE_SPACE);
55
56 /* Compact binary search. */
Arthur Heymans2fa8cab2022-10-25 21:09:58 +020057 for (i = msb; i >= 0; i--) {
58 if ((discovered | (1ULL << i)) > probe_size)
59 continue;
Patrick Rudolphbd4bcab2019-06-30 22:12:15 +020060 if (probe_mb(dram_start, (discovered | (1ULL << i))))
61 discovered |= (1ULL << i);
Arthur Heymans2fa8cab2022-10-25 21:09:58 +020062 }
Patrick Rudolphbd4bcab2019-06-30 22:12:15 +020063
64 saved_result = discovered;
65 printk(BIOS_DEBUG, "RAMDETECT: Found %zu MiB RAM\n", discovered);
66 return discovered;
67}