To answer the question in the title, the AIM 4-3-2 says:
When operating at an airport where traffic control is being exercised
by a control tower, pilots are required to maintain two-way radio
contact with the tower while operating within the Class B, Class C,
and Class D surface area unless the tower authorizes otherwise.
Initial callup should be made about 15 miles from the airport.
FWIW, I fly out of a class D and usually call 10 miles out, which gives plenty of time to contact ATC. As always, use some judgement: if the airspace is busy and the radio traffic is heavy then call further out. If it's very quiet then calling closer to the airport should be fine.
All that said, the body of your question is about being directly overhead the field. That seems like an unusual scenario (to me) but there's nothing wrong or problematic about it. I'd just call the tower, tell them where I am and that I intend to land, and let them tell me how and where to join the pattern. I can think of two practical points that might come up:
- They might assume you have some sort of emergency, so be ready to tell them you don't (unless of course that is your reason for landing there!)
- They might give you instructions to join the pattern that are difficult to comply with because they don't know what you and your aircraft are capable of. Or they might ask you how you'd like to join. Have a plan, be ready to say "unable" if you aren't comfortable with their instructions, and if necessary just tell ATC you need to fly some distance away from the airport to get down to pattern altitude.