My youngest grandson used to head bang. It was horrible while it lasted. They lived in a house with a tiled hall floor, and he used to bang his head there!? It is upsetting for those caring for him, but it is a phase some children go through. The thing is to try not to worry. Head banging is "self regulating" behaviour, and he will not bang his head severely enough to cause any damage apart from an odd bruise, he'll pull back from actually hurting himself, although it won't appear so.
By the time my grandson was about 3 he had outgrown the habit. My DD tried not to reinforce it, by leaving him to bang his head, it's a hard thing to ignore though.
To reassure you, DGS is 6 now and a very intelligent little boy.