# `NanoKVMGo.CMA`
[🔗](https://github.com/cocoa-xu/nano_kvm_go/blob/v0.4.0/lib/nano_kvm_go/cma.ex#L1)

The contiguous memory (CMA) pools of video and the NPU.

Video capture with its encoders and the NPU need physically contiguous
buffers, which Linux keeps in CMA pools: video uses the default pool, the
NPU has its own. Movable pages use what a pool does not hold, so an idle
pool costs little. The NanoKVM-Go system's U-Boot sizes both at every boot
from its environment, so new sizes apply at the next boot:

    NanoKVMGo.CMA.sizes()
    #=> %{video: 192, npu: 64}

    NanoKVMGo.CMA.persist_sizes(%{video: 160, npu: 96})
    #=> :ok

Sizes are in MiB: multiples of 8, video's at least 8, the NPU's 0 to turn
the NPU off, together leaving Linux at least 96 MiB of the DRAM.
`:default` stands for the board's size: 192 and 64 on the Go+ (512 MiB),
128 and 0 on the Go (256 MiB). 4K capture with JPEG and H.264 takes about
140 MiB with four capture buffers and 120 with three, as on the Go, counting
what else Linux keeps in the pool. On a card moved to the
other board, sizes that do not fit it give the board's, and apply again
back on the board they were set on.

The sizes for the next boots are in the U-Boot environment, through
`Nerves.Runtime.KV` or another module with its `get/1` and `put/1` (the
`:kv` option).

## Trying sizes first

With `trial: true` the next boot runs the new sizes once. Validating the
firmware, `Nerves.Runtime.validate_firmware/0`, keeps them; any boot
before that, after a crash, the watchdog or a plain reboot, goes back to
the sizes before. `trial/1` tells where a trial stands and
`cancel_trial/1` drops it.

# `persist_option`

```elixir
@type persist_option() :: {:kv, module()} | {:root, Path.t()} | {:trial, boolean()}
```

# `size`

```elixir
@type size() :: non_neg_integer() | :default
```

A pool size in MiB, or `:default` for the board's.

# `sizes`

```elixir
@type sizes() :: %{video: non_neg_integer(), npu: non_neg_integer()}
```

# `trial`

```elixir
@type trial() :: %{state: :pending | :running, sizes: %{video: size(), npu: size()}}
```

A trial of sizes: `:pending` until the boot that runs them, then
`:running`.

# `cancel_trial`

```elixir
@spec cancel_trial([{:kv, module()}]) :: :ok | {:error, term()}
```

Ends a trial of sizes: the next boots use the sizes from before it.

## Options

  * `:kv` - the U-Boot environment, `Nerves.Runtime.KV` by default

# `persist_sizes`

```elixir
@spec persist_sizes(%{optional(:video | :npu) =&gt; size()}, [persist_option()]) ::
  :ok | {:error, term()}
```

Persists pool sizes for the next boots: `:video` and `:npu` in MiB, or
`:default`. A size left out stays as it is, and a trial under way ends.

A size U-Boot would not take on this board gives `{:error,
{:invalid_size, pool, size}}`, and sizes that together leave Linux less
than 96 MiB `{:error, {:too_large, total, max}}`. A pool other than
`:video` and `:npu`, or a size that is neither a non-negative integer nor
`:default`, raises `ArgumentError`.

## Options

  * `:trial` - `true` to run the sizes at the next boot only, until the
    firmware is validated
  * `:kv` - the U-Boot environment, `Nerves.Runtime.KV` by default
  * `:root` - the root of the file system, `"/"` by default, where the
    DRAM's size comes from

# `persisted_sizes`

```elixir
@spec persisted_sizes([{:kv, module()}]) :: %{video: size(), npu: size()}
```

The sizes persisted for the next boots, `:default` for the device tree's.
During a trial, the sizes from before it: those the boots go back to unless
the firmware is validated.

## Options

  * `:kv` - the U-Boot environment, `Nerves.Runtime.KV` by default

# `sizes`

```elixir
@spec sizes([{:root, Path.t()}]) :: sizes()
```

The sizes of the pools in this boot, in MiB. The NPU's is 0 when the NPU
is off.

## Options

  * `:root` - the root of the file system to read, `"/"` by default

# `trial`

```elixir
@spec trial([{:kv, module()}]) :: trial() | nil
```

The trial of sizes, `nil` without one.

## Options

  * `:kv` - the U-Boot environment, `Nerves.Runtime.KV` by default

---

*Consult [api-reference.md](api-reference.md) for complete listing*
